2009-11-12 178 views
0

我有一個mvc應用程序。一個web項目和我使用的語言是C#。圖片上傳?

我有一個更新類別表單,並且有一個文件上傳控件,請告訴我如何執行更新功能,因爲在更新控制器中我們通常會傳遞集合對象。

請告訴我我會做什麼......我將如何做。

感謝 麗思

+2

這是一個很差的問題。 1.你使用什麼語言? 2.這是一個網頁表單還是桌面表單? 3.什麼是「收藏對象」?等等... – 2009-11-12 07:19:58

+0

請給這個問題添加更多的細節。你使用什麼編程語言? – Asaph 2009-11-12 07:20:04

+1

什麼語言?什麼框架? – Bozho 2009-11-12 07:20:10

回答

4

更改表單元素的enctypemultipart form-data

<% using (Html.BeginForm(
    "upload", 
    "controller", 
    FormMethod.Post, 
    new { enctype="multipart/form-data"} 
)) %> 

添加文件輸入到該表格:

<input type="file" name="filetoupload" id="filetoupload" /> 

和閱讀在你的控制器文件動作:

public ActionResult Upload() 
{ 
    var uploadedFile = Request.Files["filetoupload"]; 
    // TODO: do something with the uploaded file 
    return View(); 
} 
1

控制器將有一個請求屬性,它具有一個文件屬性。

foreach (string name in Request.Files) 
{ 
    HttpPostedFile file = Request.Files[name]; 

    string filePath = Path.Combine(@"C:\Somewhere", Path.GetFileName(file.FileName)); 
    file.SaveAs(filePath); 
}