Ajax AsyncFileUpload.FileBytes 返回 null

发布于 2024-08-19 06:41:11 字数 2129 浏览 8 评论 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.";
    }
}

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无所的.畏惧 2024-08-26 06:41:11

我不完全确定,但我可以想象 FileBytes 中的最大字节是有限的,因为大量文件上传会占用大量 RAM。您的托管合作伙伴可能对此进行了限制。可能您的托管服务商已将 默认设置为 512 KB。

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

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.

强辩 2024-08-26 06:41:11

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

这对我有用。
干杯
洛朗

I think I've found a fix for this problem : on the OnUploadComplete event, simply put in session the FileBytes, and retreive it in your Button_Click event. For some reason, your uploaded file bytes are erased from session after a second postback...

This works for me.
Cheers
Laurent

冬天旳寂寞 2024-08-26 06:41:11

当您使用 AsyncFileUpload 时,您必须在放置在页面或 MasterPage 中的 form 标记中设置正确的参数:

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

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

我认为您的页面中没有设置正确的 enctype。

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

When you use AsyncFileUpload you must set the right params in the form tag, that is placed in your Page or MasterPage:

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

If you don't set the right enctype and method UploadedComplete will never fire, and you won't be able to get FileUpload.FileBytes since FileUpload.HasFile returns true only during UploadedComplete execution.

I suppose that in your page you haven't set the right enctype.

Besides, prevoius versions of AsyncFileUpload didn't work on Chrome. 2011 July version (4.1.50731.0) solved the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文