大文件上传到服务器报错的问题

最近遇见了个大文件上传到服务器报错的问题;我使用的是FileUpload控件上传的

开始用的是SaveAs()和WebClient的方法,结果本地测试可以上传,一发布的服务器就出错,配置文件也写了<httpRuntime maxRequestLength="2058000" executionTimeout="90000" useFullyQualifiedRedirectUrl="false" requestLengthDiskThreshold="8192"/>但是还是不行,

郁闷了我好几天!

今天突然想到了用文件流上传FileStream

结果还真行了!

代码很简单

1.需要配置web.config里写上限制文件上传的大小就上面的那段代码;

2.在aspx.cs的文件里写:

Boolean IsReady = false;
        if (this.FileUpload1.PostedFile != null && this.FileUpload1.PostedFile.ContentLength > 0)
        {
            string path = this.Server.MapPath(@"Uploads");
            string fileName = Path.GetFileName(this.FileUpload1.PostedFile.FileName);
            int ContentLength = this.FileUpload1.PostedFile.ContentLength;
            int UploadedLength = 0;
            //uploadInfo.ContentLength = this.FileUpload1.PostedFile.ContentLength;
            //uploadInfo.FileName = fileName;
            //uploadInfo.UploadedLength = 0;

            //uploadInfo.IsReady = true;
          
            int bufferSize =1000;
            byte[] buffer = new byte[bufferSize];

            using (FileStream fs = new FileStream(Path.Combine(path, fileName), FileMode.Create))
            {
                while (UploadedLength < ContentLength)
                {
                    int bytes = this.FileUpload1.PostedFile.InputStream.Read(buffer, 0, bufferSize);
                    fs.Write(buffer, 0, bytes);
                    UploadedLength += bytes;
                }
                IsReady = true;
            }
            if(IsReady)
            {
                Response.Redirect("Default2.aspx");
            }

发布到服务器上后在本地上传大文件就没有错误了!

不过上传速度有点慢:一个400M的文件要传大概2分钟!

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值