Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在asp.net中查找单个上载文件的文件字节?_Asp.net_File_File Upload - Fatal编程技术网

如何在asp.net中查找单个上载文件的文件字节?

如何在asp.net中查找单个上载文件的文件字节?,asp.net,file,file-upload,Asp.net,File,File Upload,我正在通过asp.net文件上载控件实现多文件上载,并将上载的文件保存到数据库。在迭代文件时,我试图设置File类的Data属性。对于单个文件,我可以将其设置为file.Data=postedFile.FileBytes。 但是,在迭代文件时,每次设置的FileBytes属性值都类似。 有人能解释一下原因吗?我如何实现它 代码示例: if (fuAttachment.HasFiles) { foreach (HttpPostedFile postedFile in fuA

我正在通过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.
        }
   }

请记住,文件上载控件仍然在幕后发布文件

下面是检索多个文件的基本方法,它与一个和/或多个文件上载控件一起工作,并统计所有上载控件中所有已发布的文件:

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.
    }
}

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