Csharp/C#教程:Ajax AsyncFileUpload.FileBytes返回null分享


Ajax AsyncFileUpload.FileBytes返回null

我有一个带有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."; } } 

我不完全确定,但我可以想象FileBytes中的最大字节FileBytes是有限的,因为很多文件上传会占用大量的RAM。 您的托管合作伙伴可能限制了这一点。 可能您的主机默认情况下将为512 KB。

尝试使用SaveAs(path)保存文件。 这基本上就是你现在正在做的事情,但你会让控件弄清楚何时刷新文件,避免将整个文件存入内存,或者如果你真的需要访问原始内容则使用FileContent获取文件流。 同时将更改为102400以覆盖您的主机的默认设置。

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

这对我有用。 干杯洛朗

当您使用AsyncFileUpload您必须在form标记中设置正确的参数,该标记位于您的Page或MasterPage中:

  

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

我想在你的页面中你没有设置正确的enctype。

此外,prevoius版本的AsyncFileUpload在Chrome上不起作用。 2011年7月版(4.1.50731.0)解决了这个问题。

上述就是C#学习教程:Ajax AsyncFileUpload.FileBytes返回null分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/983451.html

(0)
上一篇 2021年12月19日
下一篇 2021年12月19日

精彩推荐