久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 C# 上傳到服務器后,Zip 文件損壞

Zip file is getting corrupted after uploaded to server using C#(使用 C# 上傳到服務器后,Zip 文件損壞)
本文介紹了使用 C# 上傳到服務器后,Zip 文件損壞的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在嘗試使用 C# (Framework 4)上傳一個 zip 文件到服務器,下面是我的代碼.

I am trying to upload a zip file to server using C# (Framework 4)and following is my code.

string ftpUrl = ConfigurationManager.AppSettings["ftpAddress"];
string ftpUsername = ConfigurationManager.AppSettings["ftpUsername"];
string ftpPassword = ConfigurationManager.AppSettings["ftpPassword"];  
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpUrl + "Transactions.zip");  
request.Proxy = new WebProxy(); //-----The requested FTP command is not supported when using HTTP proxy.
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
StreamReader sourceStream = new StreamReader(fileToBeUploaded);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
            response.Close();  

zip 文件已成功上傳,但是當我嘗試從服務器(手動)打開 zip 文件時,它顯示 Unexpected end of archive 錯誤.
對于文件壓縮,我使用 Ionic.zip dll.在傳輸 zip 文件之前,我能夠成功解壓.

The zip file is uploaded successfully, but when I tried to open the zip file from server(manually), it showed me Unexpected end of archive error.
For file compression I am using Ionic.zip dll. Before transferring the zip file, I was able to extract successfully.

任何幫助表示贊賞.謝謝.

Any help appreciated. Thanks.

推薦答案

這是問題所在:

StreamReader sourceStream = new StreamReader(fileToBeUploaded);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());

StreamReader(和任何 TextReader)用于 text 數據.zip 文件不是文本數據.

StreamReader (and any TextReader) is for text data. A zip file isn't text data.

只需使用:

byte[] fileContents = File.ReadAllBytes(fileToBeUploaded);

這樣您就不會將二進制數據視為文本,因此它不應該被破壞.

That way you're not treating binary data as text, so it shouldn't get corrupted.

或者,不要單獨將其全部加載到內存中 - 只需流式傳輸數據:

Or alternatively, don't load it all into memory separately - just stream the data:

using (var requestStream = request.GetRequestStream())
{
    using (var input = File.OpenRead(fileToBeUploaded))
    {
        input.CopyTo(requestStream);
    }
}

還請注意,您應該對所有這些流使用 using 語句,而不僅僅是調用 Close - 這樣即使出現異常,資源也會被釋放扔了.

Also note that you should be using using statements for all of these streams, rather than just calling Close - that way the resources will be disposed even if an exception is thrown.

這篇關于使用 C# 上傳到服務器后,Zip 文件損壞的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

ASP.NET Core authenticating with Azure Active Directory and persisting custom Claims across requests(ASP.NET Core 使用 Azure Active Directory 進行身份驗證并跨請求保留自定義聲明)
ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授權不起作用)
How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何獲取守護進程或服務器到 C# ASP.NET Web API 的 Azure AD OAuth2 訪問令牌和刷新令牌) - IT屋-程序員軟件開發技
Azure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(異步調用時 Azure KeyVault Active Directory AcquireTokenAsync 超時)
Getting access token using email address and app password from oauth2/token(使用電子郵件地址和應用程序密碼從 oauth2/token 獲取訪問令牌)
New Azure AD application doesn#39;t work until updated through management portal(新的 Azure AD 應用程序在通過管理門戶更新之前無法運行)
主站蜘蛛池模板: 伊人焦久影院 | 国产美女特级嫩嫩嫩bbb片 | 91精品在线看 | 日日噜噜夜夜爽爽狠狠 | 天堂网av在线 | 日本精品在线播放 | 91精品国产一区二区三区香蕉 | 美女午夜影院 | 亚洲天堂精品久久 | 日韩欧美一区二区三区免费观看 | 国产美女自拍视频 | 日韩一二区在线观看 | 欧美一级毛片免费观看 | 欧美精品一区在线观看 | 久久99精品视频 | 中文字幕一区二区三区在线观看 | 久久成人免费视频 | 欧美人人 | 久草网站 | 一区二区三区视频在线免费观看 | 欧美在线精品一区 | 911精品国产 | 91麻豆精品一区二区三区 | 国产 日韩 欧美 在线 | 亚洲精品一区中文字幕乱码 | 中文字幕电影在线观看 | 美女天天操| 天堂一区 | 欧美日韩国产精品激情在线播放 | 亚洲精品二区 | 国产成人精品一区二 | 国产精品久久久乱弄 | 国产黄色小视频在线观看 | 日本成人三级电影 | 国产成人精品一区二 | 毛片一区二区三区 | 国产精产国品一二三产区视频 | 精品久久久久久久 | 国产成人99久久亚洲综合精品 | 久久精品国产99国产 | 国产在线视频一区 |