我正在尝试从我的 Fiddle 调试器将视频文件 .mov 上传到远程 Web 服务以测试该服务,但存储在磁盘上的文件已损坏?有什么建议吗?
请求 header
Content-Type: multipart/form-data; boundary=-------------------------acebdf13572468
User-Agent: Fiddler
Host: localhost:2487
Content-Length: 2113228
请求正文
---------------------------acebdf13572468
Content-Disposition: form-data; name="IMG_0888.MOV"; filename="IMG_0888.MOV"
Content-Type: video/quicktime
<@INCLUDE *C:\Users\Amrit\Desktop\IMG_0888.MOV*@>
---------------------------acebdf13572468--
C# 代码
FileStream fs = null;
string UniqueId = this.GenerateUID();
_fileDirectory = System.IO.Path.Combine(Constants._VideosDirectory,author_id);
if (!Directory.Exists(_fileDirectory))
{
Directory.CreateDirectory(_fileDirectory);
}
string file = Path.Combine(_fileDirectory, "test.mov");
// string filePath = Path.Combine(uploadFolder, request.FileName);
try
{
using (FileStream targetStream = new FileStream(file, FileMode.Create,
FileAccess.Write, FileShare.None))
{
//read from the input stream in 65000 byte chunks
const int bufferLen = 65000;
byte[] buffer = new byte[bufferLen];
int count = 0;
while ((count = request.Read(buffer, 0, bufferLen)) > 0)
{
// save to output stream
targetStream.Write(buffer, 0, count);
}
targetStream.Close();
return "done";
//sourceStream.Close();
}
}
catch (Exception)
{
return "fail";
}
finally
{
}
最佳答案
正如 Andras 指出的那样,您可能会将整个请求保存到磁盘,而不仅仅是内容,但更有可能的是您将 unicode 字节顺序标记 (BOM) 保存到文件的开头(这是完成的)由某些 .NET 流自动执行)。
您应该做的第一件事是检查保存的文件的长度与原始文件的长度,新文件的长度可能会长约3个字节,这表明已添加了BOM。
要确认,请在十六进制编辑器中打开这两个文件并查看内容,然后将其与源文件进行比较。比较两个十六进制文件时,您应该只查看前几个字符和最后几个字符(如果最后一个字符不同,则意味着您的流未完成并且文件被截断,如果第一个字符不同,则意味着您有 BOM附后)。
您可能需要使用的是 File.WriteBytes
而不是 FileStream
,这将阻止包含 BOM。
关于c# - 二进制上传 WCF 损坏的 .mov 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10318760/