ASP.NET MVC 上傳檔案

來源:互聯網
上載者:User

這幾天練習的ASP.NET MVC項目中碰到了檔案上傳的問題,分享一下:

View:

{
this.style.display='none'; document.getElementById('Code_Closed_Text_145457').style.display='none'; document.getElementById('Code_Open_Image_145457').style.display='inline'; document.getElementById('Code_Open_Text_145457').style.display='inline';
}" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" alt="" width="11" height="16" align="top">{
this.style.display='none'; document.getElementById('Code_Open_Text_145457').style.display='none'; getElementById('Code_Closed_Image_145457').style.display='inline'; getElementById('Code_Closed_Text_145457').style.display='inline';
}" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" alt="" width="11" height="16" align="top">View
1<form action="/AdminImage/Index/" method="post" enctype="multipart/form-data">
2    <div>
3        Please select a file to upload:&nbsp;&nbsp;
4        <input type="file" id="fileImage" name="fileImage" />&nbsp;&nbsp;
5        <input type="submit" value="Upload" />
6    </div>
7</form>

 

Controller:

{
this.style.display='none'; document.getElementById('Code_Closed_Text_145657').style.display='none'; document.getElementById('Code_Open_Image_145657').style.display='inline'; document.getElementById('Code_Open_Text_145657').style.display='inline';
}" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" alt="" width="11" height="16" align="top">{
this.style.display='none'; document.getElementById('Code_Open_Text_145657').style.display='none'; getElementById('Code_Closed_Image_145657').style.display='inline'; getElementById('Code_Closed_Text_145657').style.display='inline';
}" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" alt="" width="11" height="16" align="top">Controller
 1        public ActionResult Index()
 2{
this.style.display='none'; document.getElementById('Codehighlighter1_44_82_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_44_82_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_44_82_Closed_Text').style.display='inline';
}" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" alt="" align="top">{
this.style.display='none'; document.getElementById('Codehighlighter1_44_82_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_44_82_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_44_82_Open_Text').style.display='inline';
}" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" alt="" align="top">        {
 3            return View();
 4        }
 5        [AcceptVerbs(HttpVerbs.Post)]
 6        public ActionResult Index(FormCollection collection)
 7{
this.style.display='none'; document.getElementById('Codehighlighter1_190_745_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_190_745_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_190_745_Closed_Text').style.display='inline';
}" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" alt="" align="top">{
this.style.display='none'; document.getElementById('Codehighlighter1_190_745_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_190_745_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_190_745_Open_Text').style.display='inline';
}" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" alt="" align="top">        {
 8            if (Request.Files.Count == 0)
 9{
this.style.display='none'; document.getElementById('Codehighlighter1_246_292_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_246_292_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_246_292_Closed_Text').style.display='inline';
}" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" alt="" align="top">{
this.style.display='none'; document.getElementById('Codehighlighter1_246_292_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_246_292_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_246_292_Open_Text').style.display='inline';
}" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" alt="" align="top">            {
10                return View();
11            }
12            var c = Request.Files[0];
13            if (c != null && c.ContentLength > 0)
14{
this.style.display='none'; document.getElementById('Codehighlighter1_393_708_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_393_708_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_393_708_Closed_Text').style.display='inline';
}" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" alt="" align="top">{
this.style.display='none'; document.getElementById('Codehighlighter1_393_708_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_393_708_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_393_708_Open_Text').style.display='inline';
}" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" alt="" align="top">            {
15                int lastSlashIndex = c.FileName.LastIndexOf("//");
16                string fileName = c.FileName.Substring(lastSlashIndex + 1, c.FileName.Length - lastSlashIndex - 1);
17                fileName = Path.Combine(CommonUtility.DocImagePath, fileName);
18                c.SaveAs(fileName);
19            }
20            return View();
21        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.