簡體   English   中英

圖片上傳?

[英]image upload?

我有一個mvc application。一個Web項目,我使用的語言是C#。

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

請告訴我該怎么辦..以及我將如何做。

謝謝麗茲

將form元素的enctype更改為multipart 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();
}

控制器將具有一個Request屬性,該屬性具有一個Files屬性。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM