Posting Zip Files over HTTP No Longer Opening in Win 7 Zip Program
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 Htt开发者_开发知识库pWebResponse;
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.
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, and HttpPostedFile.SaveAs
to save the file to disk.
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.
精彩评论