我有以下用于处理文件帖子的方法:
public string Message(Stream data)
{
int length = 0;
using (FileStream writer = new FileStream("test.zip", FileMode.Create))
{
int readCount;
var buffer = new byte[8 * 16];
while ((readCount = data.Read(buffer, 0, buffer.Length)) != 0)
{
writer.Write(buffer, 0, readCount);
length += readCount;
}
}
return "test";
}
使用这个我可以发布邮递员的文件。当我检查另一侧发布的zip文件时,一切看起来都不错,可以打开zip文件而不会出错。这是我用来从C#客户端发布文件的代码:
using (WebClient clientFile = new WebClient())
{
clientFile.UploadFileAsync(new Uri("http://192.168.122.146:8000/Message"), "test.zip");
}
文件已上传但当我想打开时会显示错误消息:“压缩(压缩)文件夹无效或已损坏”。有没有办法上传文件而不使用multipart / form-data?该代码适用于邮递员但如果我从C#客户端上传文件则无效。 以下是来自Fiddler的请求:
POST http://192.168.122.146:8000/Message HTTP/1.1
Host: 192.168.122.146:8000
Connection: keep-alive
Content-Length: 8505677
Cache-Control: no-cache
Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop
User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36
Postman-Token: 4d0e5489-7818-226f-6f71-b9bee2947905
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
来自C#客户端的请求:
POST http://192.168.122.146:8000/PutMessage HTTP/1.1
Content-Type: multipart/form-data; boundary=---------------------8d4ef8a23bf0f80
Host: 192.168.122.146:8000
Content-Length: 8505848
Expect: 100-continue
Connection: Keep-Alive
-----------------------8d4ef8a23bf0f80
Content-Disposition: form-data; name="file"; filename="test.zip"