阿贾克斯AsyncFileUpload.FileBytes返回nullAsyncFileUpload、阿贾克斯、null、FileBytes

2023-09-11 22:31:18 作者:陈喻老司机.

我有一个AsyncFileUpload控制文件上传页面。当用户浏览到该文件上载控制拉文件到内存中。然后我有触发以下code将文件保存到数据库中的上传按钮。

我发现,如果文件是在大约500KB则控制的FileBytes属性只是返回null。这发生在我的服务器上,但在运行应用程序时,在本地运行经过精细。

我不处理OnUploadCompleted事件,因为我需要将用户提交的文件数据库之前,完成更多的信息。

我有这个在我的web.config:的httpRuntime maxRequestLength的=10000/>

 私人无效UploadDocument(INT mietID)
{
    如果(Page.IsValid)
    {
        如果(mietID大于0)
        {
            如果(File1.HasFile&安培;&安培;!File1.FileBytes = NULL)
            {
                字符串[] docFormats = MIETPConfig.Current.SupportedDocumentFormats;

                对于(简称I = 0; I< docFormats.Length;我++)
                    docFormats [I] = docFormats [I] .ToUpper();

                如果(docFormats.Contains(Path.GetExtension(File1.FileName).ToUpper()))
                {
                    尝试
                    {
                        byte []的uploadedBytes = File1.FileBytes;
                        DocumentController.CreateDocument(txtLinkText.Text,Path.GetFileName(File1.PostedFile.FileName),uploadedBytes,mietID,(用户)会话[用户]);

                        MietpClientScripts.CloseWindow(页);
                    }
                    赶上(例外)
                    {
                        lblUploadStatus.Text =发生错误保存文档到数据库中。
                    }
                }
                其他
                {
                    System.Text.StringBuilder SB =新System.Text.StringBuilder();
                    的foreach(字符串s的docFormats)
                        sb.Append(S +,);

                    sb.Remove(sb.Length  -  2,2);
                    lblUploadStatus.Text =无效的文件格式,只有这些格式的支持:+ sb.ToString();
                }
            }
            其他
            {
                lblUploadStatus.Text =发生错误保存文档时,文档无法被读取,它可能过大而无法上传。
            }
        }
        其他
            lblUploadStatus.Text =没有Mietp的ID文件与相关联。
    }
}
 

解决方案

我不能完全肯定,但我可以想像,最大的 FileBytes 字节由于被限制事实上,文件上传的很多会占用大量的内存。你hostingpartner可能限制了这一点。也许您的主机托管服务提供商设置<的httpRuntime maxRequestLength的=XXX/> 512 KB的默认

尽量节约使用另存为(路径)文件。这基本上是你在做什么,在这一点上,但你让控制弄清楚何时刷新该文件,以避免整个文件在内存中,或使用抢FILESTREAM FileContent 如果你真的需要访问原始内容。同时更改<的httpRuntime maxRequestLength的=XXX/> 来像 102400 从覆盖默认设置你的主机托管服务提供商。

I have a file upload page with an AsyncFileUpload control. When the user browses to the file the upload control pulls the file into memory. I then have an Upload button which fires the following code to save the file to a database.

c ftp上传第一个文件成功上传第二个的时候失败

I am finding that if files are over about 500KB then the FileBytes property of the control simply returns null. This happens on my server but when running the app locally it runs through fine.

I'm not handling the OnUploadCompleted event as I need to user to complete further information before committing the file to database.

I have this in my web.config: httpRuntime maxRequestLength="10000"/>

private void UploadDocument(int mietID)
{
    if (Page.IsValid)
    {
        if (mietID > 0)
        {
            if (File1.HasFile && File1.FileBytes != null)
            {
                string[] docFormats = MIETPConfig.Current.SupportedDocumentFormats;

                for (short i = 0; i < docFormats.Length; i++)
                    docFormats[i] = docFormats[i].ToUpper();

                if (docFormats.Contains(Path.GetExtension(File1.FileName).ToUpper()))
                {
                    try
                    {
                        byte[] uploadedBytes = File1.FileBytes;
                        DocumentController.CreateDocument(txtLinkText.Text, Path.GetFileName(File1.PostedFile.FileName), uploadedBytes, mietID, (User)Session["User"]);

                        MietpClientScripts.CloseWindow(Page);
                    }
                    catch (Exception)
                    {
                        lblUploadStatus.Text = "There was an error saving the document to the database.";
                    }
                }
                else
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    foreach (string s in docFormats)
                        sb.Append(s + ", ");

                    sb.Remove(sb.Length - 2, 2);
                    lblUploadStatus.Text = "Invalid file format, only these formats are supported: " + sb.ToString();
                }
            }
            else
            {
                lblUploadStatus.Text = "There was an error saving the document, the document could not be read; it might be too large to upload.";
            }
        }
        else
            lblUploadStatus.Text = "No Mietp ID to associate document with.";
    }
}

解决方案

I am not totally sure, but I can imagine that the max bytes in FileBytes is limited due to the fact that alot of file uploads would take up a lot of RAM. Your hostingpartner might have limited this. Probably your hoster has set the <httpRuntime maxRequestLength="XXX" /> to 512 KB by default.

Try saving the file using SaveAs(path). This is basically what you are doing at this point, but you'll let the control figure out when to flush to the file, avoiding taking the whole file in memory, or grab the filestream using FileContent if you really need access to the raw content. Also change <httpRuntime maxRequestLength="XXX" /> to something like 102400 to override the default settings from your hoster.