通过 HTTP 发布 Zip 文件不再在 Win 7 Zip 程序中打开

发布于 2024-10-14 17:29:46 字数 2124 浏览 4 评论 0原文

我有两段代码。一个上传 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

匿名。 2024-10-21 17:29:47

我相信问题出在使用 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, and HttpPostedFile.SaveAs to save the file to disk.

岁月染过的梦 2024-10-21 17:29:47

您正在编写整个请求,其中可能包含一些多部分 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文