1

我有一个

<input type="file" id="files" name="files[]" multiple runat="server" />

我不使用 asp:FileUpload 因为我需要使用 jQuery 插件来预览多个图像并在上传之前删除图像。

我的问题是如何管理来自代码后面的输入数据?我必须遍历选定的图像。我在网上搜索并没有发现任何有趣的东西......

如果我尝试,从后面的代码以这种方式阅读:

 HttpPostedFile file = Request.Files["files[]"];

我看到 Request.Files.Count 始终为 0。

提前致谢!

4

1 回答 1

0

老实说,快速搜索给了我这个:

在你的 aspx 中:

<form id="form1" runat="server" enctype="multipart/form-data">
 <input type="file" id="myFile" name="myFile" />
 <asp:Button runat="server" ID="btnUpload" OnClick="btnUploadClick" Text="Upload" />
</form>

在后面的代码中:

protected void btnUploadClick(object sender, EventArgs e)
{
    HttpPostedFile file = Request.Files["myFile"];

    //check file was submitted
    if (file != null && file.ContentLength > 0)
    {
        string fname = Path.GetFileName(file.FileName);
        file.SaveAs(Server.MapPath(Path.Combine("~/App_Data/", fname)));
    }
}

更新

另一个快速搜索给了我这个,如果你有多个文件,那么获取它们的解决方案是:

for (int i = 0; i < Request.Files.Count; i++)
{
    HttpPostedFileBase file = Request.Files[i];
    if(file .ContentLength >0){
    //saving code here

 }
于 2017-01-27T22:48:26.647 回答