Archive or image is corrupted after uploading to FTP and downloading back with FtpWebRequest(上传到 FTP 并使用 FtpWebRequest 下载回来后,存档或图像已损坏)
问题描述
我有两种方法:
- 上传文件到 FTP 服务器
- 从服务器下载文件.
一切都与文本或 xml 文件完美配合.但是,当我尝试上传然后下载存档或图像时,我收到Windows 无法打开文件夹.压缩的 zip 文件无效"错误,存档和图像几乎相同.可能是什么问题?
Everything works perfectly with text or xml files. But when I'm trying to upload and then download an archive or an image I get the "windows cannot open the folder. the compressed zip file is invalid" error for the archives and almost the same for the images. What may be the problem?
这是我的方法列表:
上传:
private string Upload(string Login, string Password, string FilePath, string FileName, string uuid, string FTPDir)
{
string CreateDirectory = CreateFTPDirectory(Login, Password, uuid, FTPDir);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(@"ftp://" + FTPDir + uuid + "/" + FileName);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UseBinary = true;
StreamReader sourceStream = new StreamReader(FilePath + FileName);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
using (Stream S = request.GetRequestStream())
{
S.Write(fileContents, 0, fileContents.Length);
}
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
return response.StatusDescription;
}
下载:
private string Download(string Login, string Password, string FileName, string uuid, string FTPDir, string Destination)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + FTPDir + uuid + "/" + FileName);
request.UseBinary = true;
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(Login, Password);
byte[] buffer = new byte[1024];
using (var response = (FtpWebResponse)request.GetResponse())
{
using (var stream = response.GetResponseStream())
{
using (var fs = new FileStream(Destination, FileMode.OpenOrCreate))
{
int readCount = stream.Read(buffer, 0, 1024);
while (readCount > 0)
{
fs.Write(buffer, 0, readCount);
readCount = stream.Read(buffer, 0, 1024);
}
}
return response.StatusDescription;
}
}
}
推荐答案
您正在上传二进制文件(位图图像),就好像它是 UTF-8 编码的文本文件一样:
You are uploading a binary file (a bitmap image) as if it were a text file in UTF-8 encoding:
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
这自然会损坏文件.
你必须一点一点地传输二进制文件.
You have to transfer binary files exactly as they are, bit by bit.
此外,您的技术对于可能较大的图像文件效率很低.您将整个文件至少保存在内存中两次.
Moreover your technique is quite inefficient for potentially large image files. You keep whole file in memory at least twice.
您需要的代码实际上比您的简单得多:
The code, that you need, is actually much simpler than yours:
using (Stream fileStream = File.OpenRead(FilePath + FileName)
using (Stream ftpStream = request.GetRequestStream())
{
fileStream.CopyTo(ftpStream);
}
<小时>
你的下载代码没问题,但同样可以简化为:
Your download code is ok, but again, it can be simplified to:
using (Stream ftpStream = request.GetResponse().GetResponseStream())
using (Stream fileStream = File.Create(Destination))
{
ftpStream.CopyTo(fileStream);
}
<小时>
有关完整代码,请参阅在 C#/.NET 中向/从 FTP 服务器上传和下载二进制文件.
这篇关于上传到 FTP 并使用 FtpWebRequest 下载回来后,存档或图像已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:上传到 FTP 并使用 FtpWebRequest 下载回来后,存档或
基础教程推荐
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30