【问题标题】:asp.net and input type=file multiple read data from code behindasp.net 和 input type=file 从后面的代码中多次读取数据
【发布时间】:2017-01-27 22:38:10
【问题描述】:

我有一个

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

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

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

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

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

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

提前致谢!

【问题讨论】:

  • 我想如果数据作为表单的一部分发布,它应该仍然在 Request.Files 集合中。无论是否是服务器端控件,HTTP 请求中的所有内容都应该在 Request 上。
  • 我尝试使用 HttpPostedFile file = Request.Files["files[]"];并使用 HttpPostedFile file = Request.Files["files"];但它始终为空
  • 调试的时候Request.Files里面有什么东西吗?在尝试为事物分配随机索引之前检查类型。
  • 不,Request.Files 的计数是 0...
  • 如果 Request.Files 的长度始终为 0,您确定文件是首先发布的吗?检查浏览器调试工具中的网络选项卡。请求是否包含文件?

标签: c# asp.net file-upload multiple-file-upload httppostedfile


【解决方案1】:

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

在你的 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

 }

【讨论】:

  • 但我有多个文件
  • @Martina 所以在标签中添加多个?就像使用文件上传控制一样简单。
  • 如果您阅读了我的问题,我已经说过我有
  • @Martina 不,你甚至不知道你在问什么?你想在你的代码隐藏中管理它的输入标签,那么你的答案是HttpPostedFile 以及如何解析它,
  • @Martina 很高兴你找到它 =)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-04
  • 2013-03-16
  • 1970-01-01
  • 1970-01-01
  • 2013-04-19
  • 2011-12-31
相关资源
最近更新 更多