日期:2014-05-17  浏览次数:21145 次

Stream分段写入提示“请求被中止: 请求已被取消”
为了实现一个文件上传的功能,我将上传的文件分段写入Stream类的对象,如果文件过大(貌似大于3M)就会提示“请求被中止: 请求已被取消”,如果不分段的话就能成功上传不提示任何问题。看代码

                int UploadNum=0;
                Stream dataStream = request.GetRequestStream();
                UploadNum = postData.Length / 5000;
                System.Threading.Timer FileTm = new System.Threading.Timer(CalculateSpeedTime, null, 0, 1500);
                for (int i = 0; i <= UploadNum; i++)
                {
                    if (i == UploadNum)
                    {
                        dataStream.Write(postData, i * 5000, postData.Length - i * 5000);
                        TempDataSize += postData.Length - i * 5000;
                    }
                    else
                    {
                        dataStream.Write(postData, i * 5000, 5000);//这里会报错
                        TempDataSize += 5000;
                    }
                    setprogressbar(true, (i * 5000.0 / postData.Length) * 100.0, UploadSpeed);
                }
                dataStream.Close();