0

我有一个带有 AsyncFileUpload 控件的文件上传页面。当用户浏览文件时,上传控件将文件拉入内存。然后我有一个上传按钮,它会触发以下代码以将文件保存到数据库。

我发现如果文件超过 500KB,那么控件的 FileBytes 属性只会返回 null。这发生在我的服务器上,但是在本地运行应用程序时,它运行良好。

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

我的 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.";
    }
}
4

3 回答 3

2

我不完全确定,但我可以想象最大字节FileBytes数是有限的,因为大量文件上传会占用大量 RAM。您的托管合作伙伴可能已对此进行了限制。默认情况下,您的主机可能已将 512 KB 设置<httpRuntime maxRequestLength="XXX" />为 512 KB。

尝试使用保存文件SaveAs(path)。这基本上是您此时正在做的事情,但是您将让控件确定何时刷新到文件,避免将整个文件放入内存,或者FileContent如果您确实需要访问原始内容,则使用获取文件流。还可以更改<httpRuntime maxRequestLength="XXX" />102400覆盖托管服务商的默认设置。

于 2010-01-25T16:35:11.893 回答
2

我想我已经找到了解决这个问题的方法:在 OnUploadComplete 事件中,只需将 FileBytes 放入会话中,然后在 Button_Click 事件中检索它。出于某种原因,您上传的文件字节会在第二次回发后从会话中删除...

这对我有用。干杯劳伦特

于 2010-02-08T18:37:31.233 回答
2

使用时,AsyncFileUpload您必须在form标签中设置正确的参数,即放置在您的 Page 或 MasterPage 中:

 <form id="form1" runat="server" enctype="multipart/form-data" method="post">

如果您没有设置正确的 enctype 和方法UploadedComplete 将永远不会触发,并且您将无法获取FileUpload.FileBytes因为FileUpload.HasFile仅在 UploadedComplete 执行期间返回 true。

我想在您的页面中您没有设置正确的编码类型。

此外,之前版本的 AsyncFileUpload 无法在 Chrome 上运行。2011 年 7 月版 (4.1.50731.0) 解决了这个问题。

于 2011-10-06T14:20:53.587 回答