我正在尝试使用 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 使用 Azure Active Directory 进行身份验证并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 令牌授权不起作用ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授权不起作用)
如何获取守护进程或服务器到 C# ASP.NET Web API 的How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何获取守护进程或服务器到 C# ASP.N
异步调用时 Azure KeyVault Active Directory AcquireTokenAAzure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(异步调用时 Azure KeyVault Active Directory AcquireTokenAsync 超
使用电子邮件地址和应用程序密码从 oauth2/tokenGetting access token using email address and app password from oauth2/token(使用电子邮件地址和应用程序密码从 oauth2/token 获取访问令
新的 Azure AD 应用程序在通过管理门户更新之前无New Azure AD application doesn#39;t work until updated through management portal(新的 Azure AD 应用程序在通过管理门户更新之前无法运行