久久久久久久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 應用程序在通過管理門戶更新之前無法運行)
主站蜘蛛池模板: 最新日韩在线 | 久久亚洲欧美日韩精品专区 | 日一区二区三区 | 国内毛片毛片毛片毛片 | 操到爽| 成人在线视频一区 | 久在线视频播放免费视频 | 亚洲日日夜夜 | 日日操av| 国产视频1 | 日韩一区二区三区在线观看 | 极品一区 | 亚洲一区二区三区四区五区午夜 | 91精品国产自产在线老师啪 | aaaaaa大片免费看最大的 | 成人在线精品视频 | 国产精品精品久久久 | 国产二区精品视频 | 北条麻妃99精品青青久久 | 一起操网站 | 男女在线免费观看 | 欧美一区精品 | 精品欧美一区免费观看α√ | 999久久久免费精品国产 | 九九精品在线 | 国产玖玖 | 久久精品亚洲精品国产欧美 | 国产人久久人人人人爽 | 本地毛片 | 日韩精品1区2区3区 国产精品国产成人国产三级 | 久久久久久国产精品 | 亚洲精品中文字幕在线观看 | 久久成人18免费网站 | 一级黄色片日本 | av一级 | 91在线精品视频 | 精品国产一区二区久久 | 中文字幕一区二区三区精彩视频 | 国产福利在线视频 | 午夜精品久久久久久久久久久久久 | 亚洲一区二区电影在线观看 |