【发布时间】:2011-06-17 13:04:30
【问题描述】:
我有两段代码。一个上传 zipfile 和一个将上传文件保存到驱动器的服务器。我的问题是我上传了一个 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" 内容类型:application/octet-stream
所以我的问题是如何在不捕获标题信息的情况下保存 zip。
【问题讨论】:
标签: c# asp.net file-upload upload zip