問題描述
我有兩種方法:
- 上傳文件到 FTP 服務(wù)器
- 從服務(wù)器下載文件.
一切都與文本或 xml 文件完美配合.但是,當(dāng)我嘗試上傳然后下載存檔或圖像時,我收到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.
此外,您的技術(shù)對于可能較大的圖像文件效率很低.您將整個文件至少保存在內(nèi)存中兩次.
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);
}
<小時>
有關(guān)完整代碼,請參閱在 C#/.NET 中向/從 FTP 服務(wù)器上傳和下載二進制文件.
這篇關(guān)于上傳到 FTP 并使用 FtpWebRequest 下載回來后,存檔或圖像已損壞的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!