2017-02-13 87 views
0

嗨我需要編寫一個程序來存儲數據庫中的文件路徑,並將.doc文件存儲在一個新的文件位置下的文件名相同的名稱。我不知道如何爲它編寫代碼。文件上傳mvc 4 web應用程序到服務器無模式

[HttpPost] 
public ActionResult Upload(HttpPostedFileBase file) 
{ 
    if (file != null && file.ContentLength > 0) 
    { 
     var fileName = Path.GetFileName(file.FileName); 
     var path = Path.Combine(Server.MapPath("~/SaveDoc/"), fileName); 
     file.SaveAs(path); } 
     return RedirectToAction("UploadDocument"); 
    } 
} 

觀點::

@using (Html.BeginForm("Upload", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    <input type="file" name="FileUpload" /> 
    <input type="submit" name="Submit" id="Submit" value="Upload" /> } 
+0

請參考[動態創建目錄並將圖像保存到文件夾中](http://www.c-sharpcorner.com/blogs/create-directory-dynamically-and-save-image-to-folder-in-asp- net1) –

+0

我使用Razor引擎,我相信.asp不會被支持,你能否以另一種方式幫助我(jqwidgets,boostrap,jquery等) – Cyborgsri

+0

你寫的程序是存儲文件的基本和正確的程序 –

回答

0

我測試您的解決方案,並與一些調整的工作原理:

  1. 確保在你的情況下,控制器被稱爲上傳
  2. 名稱=視圖中的「FileUpload」必須與控制器中方法的參數相匹配
  3. Th e提交按鈕不需要具有值屬性。

我做了這些改變,一切正常。

相關問題