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