通过 HTTP 发布 Zip 文件不再在 Win 7 Zip 程序中打开
我有两段代码。一个上传 zip 文件,一个服务器将上传内容保存到驱动器。我的问题是我上传了一个 zip 文件,该文件在 Windows 7 默认压缩程序中可以正常打开,但是当我尝试从网络服务器打开它时,它也被发布,它不会再打开,并出现错误:
Windows 无法打开该文件夹。压缩文件夹“blah”无效。
注1:该文件在WinRar或其他zip程序中完全可以正常打开。
注2:原始文件和服务器上的文件在磁盘上的大小完全相同,但其中一个文件的大小服务器大了 200 字节
这是上传 zip 的代码:
public static String UploadFile(String url, String filePath)
{
if (!File.Exists(filePath))
throw new FileNotFoundException();
try
{
using (var client = new WebClient())
{
byte[] result = client.UploadFile(url, filePath);
UTF8Encoding enc = new UTF8Encoding();
string response = enc.GetString(result);
return response;
}
}
catch (WebException webException)
{
HttpWebResponse httpWebResponse = webException.Response as HttpWebResponse;
return (httpWebResponse == null) ? webException.Message : httpWebResponse.StatusCode.ToString();
}
}
这是服务器上保存传入文件的代码(存在于 .NET C# aspx 页面的页面加载中):
private void SaveZipFile()
{
string fileName;
string zipPath;
fileName = GenerateFileName();
zipPath = _hhDescriptor.GetDirectory(path => Server.MapPath(("./" + _serviceName + "\\" + path)) + "\\" + fileName + ".zip");
if (!Directory.Exists(zipPath))
{
Directory.CreateDirectory(Path.GetDirectoryName(zipPath));
}
Request.SaveAs(zipPath, false);
logger.Trace(string.Format("ManualUpload: Successfully saved uploaded zip file to {0}", zipPath));
}
任何想法/或建议这可能会破坏的可能地方将不胜感激!我可能会随 zip 文件一起保存一些其他随机内容。
更新1:
当我在记事本中打开服务器的zip文件时,它包含
------------------------8cd8d0e69a0670b 内容处置:表单数据; 名称=“文件”;文件名=“文件名.zip” 内容类型:应用程序/八位字节流
所以我的问题是如何在不捕获标头信息的情况下保存 zip。
I have two bits of code. One which uploads a zipfile and a server which saves the upload to the drive. My problem is I upload a zip file which opens fine in the windows 7 default zipping program, but when I try to open it from the webserver it was posted too it won't open anymore with the error:
Windows cannot open the folder. The compressed zipped folder 'blah' is invalid.
Note1: The file opens completely fine in WinRar or other zip programs.
Note2: The original file and the file on the server are the exact same size on disk but the size of the one of the server is 200ish bytes bigger
Here is the code for uploading zips:
public static String UploadFile(String url, String filePath)
{
if (!File.Exists(filePath))
throw new FileNotFoundException();
try
{
using (var client = new WebClient())
{
byte[] result = client.UploadFile(url, filePath);
UTF8Encoding enc = new UTF8Encoding();
string response = enc.GetString(result);
return response;
}
}
catch (WebException webException)
{
HttpWebResponse httpWebResponse = webException.Response as HttpWebResponse;
return (httpWebResponse == null) ? webException.Message : httpWebResponse.StatusCode.ToString();
}
}
Here is the code on the server which saves the incoming file (exists in the page load of a .NET C# aspx page):
private void SaveZipFile()
{
string fileName;
string zipPath;
fileName = GenerateFileName();
zipPath = _hhDescriptor.GetDirectory(path => Server.MapPath(("./" + _serviceName + "\\" + path)) + "\\" + fileName + ".zip");
if (!Directory.Exists(zipPath))
{
Directory.CreateDirectory(Path.GetDirectoryName(zipPath));
}
Request.SaveAs(zipPath, false);
logger.Trace(string.Format("ManualUpload: Successfully saved uploaded zip file to {0}", zipPath));
}
Any ideas / or suggestions as possible places this could be breaking would be greatly appreciated!. I am probably saving some other random stuff along with the zip file.
UPDATE 1:
When I open the server's zip file in notepad it contains
-----------------------8cd8d0e69a0670b Content-Disposition: form-data;
name="file"; filename="filename.zip"
Content-Type: application/octet-stream
So my question is how to save the zip without capturing the header info.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信问题出在使用
HttpRequest.SaveAs
。我怀疑这会保存整个请求,包括 HTTP 标头。在二进制文件编辑器中查看该文件,我怀疑您会在开头找到标头。使用
HttpRequest.Files
获取作为请求的一部分上传的文件,以及HttpPostedFile.SaveAs
将文件保存到磁盘。I believe that the problem is using
HttpRequest.SaveAs
. I suspect that's saving the entire request, including the HTTP headers. Look at the file in a binary file editor and I suspect you'll find the headers at the start.Use
HttpRequest.Files
to get at files uploaded as part of the request, andHttpPostedFile.SaveAs
to save the file to disk.您正在编写整个请求,其中可能包含一些多部分 MIME 分隔符。我认为您需要使用 Request.Files< /strong>。
You are writing the entirety of the request which may have some Multipart MIME separators in it. I think you need to use Request.Files.