gpt4 book ai didi

c# - 无法上传大小超过 5000 k 的 zip 文件

转载 作者:太空宇宙 更新时间:2023-11-03 17:37:23 24 4
gpt4 key购买 nike

我是 C# 的新手,我能够上传一个较小的文件到服务器 但是当我尝试上传超过 5000k 的大小时,它给出了一个异常(exception)。

这是我的C#代码

private void UploadFile(string filename)
{
try
{
PeopleMatrixService peopleMetrixService = new PeopleMatrixService();

String strFile = System.IO.Path.GetFileName(filename);
// TestUploader.Uploader.FileUploader srv = new TestUploader.Uploader.FileUploader();
FileInfo fInfo = new FileInfo(filename);
long numBytes = fInfo.Length;
double dLen = Convert.ToDouble(fInfo.Length / 10000000);
if (dLen < 8)
{
FileStream fStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fStream);
byte[] data = br.ReadBytes((int)numBytes);
br.Close();
string sTmp = peopleMetrixService.UploadFile(data, strFile);
fStream.Close();
fStream.Dispose();
MessageBox.Show("File Upload Status: " + sTmp, "File Upload");
}
else
{
MessageBox.Show("The file selected exceeds the size limit for uploads.", "File Size");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Upload Error");
}
}

和我在网络服务中的代码

[WebMethod]
public string UploadFile(byte[] f, string fileName)
{
try
{
MemoryStream ms = new MemoryStream(f);
FileStream fs = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath("~/Response Attachments/") + fileName, FileMode.Create);
//FileStream fs = new FileStream(Server.MapPath("~/Response Attachments/") + fileName, FileMode.Create);
ms.WriteTo(fs);
ms.Close();
fs.Close();
fs.Dispose();
return "OK";
}
catch (Exception ex)
{
return ex.Message.ToString();
}
}

最佳答案

默认情况下,maxRequestLength 设置为 4096 (4mb),您需要在 web.config 文件中将其更改为更大的大小。

参见:http://msdn.microsoft.com/en-us/library/e1f13641(VS.80).aspx

关于c# - 无法上传大小超过 5000 k 的 zip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/680339/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com