问题描述
- 大家帮帮忙!我这个上传文件的方法为什么会限制文件的大小啊?文件超过一定的大小,上传就失败了!
-
private void PostFile(string filename) { FileStream stream = new FileInfo(filename).OpenRead(); var shortfilename = Path.GetFileName(filename); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(""http://120.24.97.123/APPService.svc/UploadFile/"" + OperationOrderID + ""/"" + shortfilename); request.Method = ""POST""; //request.ContentType = ""application/json;charset=UTF-8""; //request.ContentType = ""application/x-www-form-urlencoded""; request.ContentLength = stream.Length; Stream requestStram = request.GetRequestStream(); int bytesread = 0; //从流中读取字节块并将该数据写入给定缓冲区buffer中 Byte[] buffer = new Byte[4096]; bytesread = stream.Read(buffer 0 4096); long UploadByte = bytesread; while (bytesread != 0) { if (bytesread == 4096) { requestStram.Write(buffer 0 4096); } else { requestStram.Write(buffer 0 bytesread); } SetText(shortfilename + "" 文件正在上传中("" + FormatFileSize(UploadByte) + ""/"" + FormatFileSize(stream.Length) + "")""); bytesread = stream.Read(buffer 0 4096); UploadByte = UploadByte + bytesread; } stream.Close(); stream.Dispose(); requestStram.Close(); requestStram.Dispose(); SetText(shortfilename + "" 文件上传成功...""); Stream getStream = request.GetResponse().GetResponseStream(); //StreamReader smd = new StreamReader(getStream); //SetText(smd.ReadToEnd()); //byte[] resultByte = new byte[200]; request.Abort(); //getStream.Read(resultByte 0 resultByte.Length); //Console.WriteLine(Encoding.UTF8.GetString(resultByte)); //GetFiles(); }
解决方案
解决方案三:
是不是request.contentlength和4096冲突,不匹配造成的?
时间: 2024-12-10 11:37:09