【问题标题】:How to find filebytes of individual uploaded files in asp.net?如何在 asp.net 中查找单个上传文件的文件字节数?
【发布时间】:2014-09-21 15:36:25
【问题描述】:

我正在通过 asp.net 文件上传控件实现多文件上传并将上传的文件保存到数据库。在迭代文件时,我试图设置 File 类的 Data 属性。对于单个文件,我可以将其设置为 File.Data = postedFile.FileBytes。 但是,在迭代文件时,每次都设置 FileBytes 属性值相似。 谁能解释可能是什么原因?如何实现?

代码示例:

 if (fuAttachment.HasFiles)
   {
        foreach (HttpPostedFile postedFile in fuAttachment.PostedFiles)
        {
            DataAccess.File file = new DataAccess.File();
            file.Data = fuAttachment.FileBytes;//not working for multiple files.
        }
   }

【问题讨论】:

  • 你不应该从postedFile读取文件的内容吗?

标签: asp.net file file-upload


【解决方案1】:

请记住,文件上传控件仍会在后台发布文件。

这里基本上是如何检索多个文件,这适用于一个和/或多个文件上传控件,并在 ALL 的上传控件中计算 ALL 已发布的文件:

HttpFileCollection uploadedFiles = Request.Files;
for (int i = 0; i < uploadedFiles.Count; i++)
{
    HttpPostedFile userPostedFile = uploadedFiles[i];
    try
    {
        if (userPostedFile.ContentLength > 0)
        {
           //do stuff with your file, check the attributes and functions of 'userPostedFile' to see what you can get from it, there is an input stream as well as file name among everything else.
        }
    }
    catch (Exception Ex)
    {
         //here do stuff if file upload fails, adjust exception type as needed.
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-24
    • 1970-01-01
    • 2018-08-30
    • 2019-01-29
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多