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

上傳到 FTP 并使用 FtpWebRequest 下載回來后,存檔或

Archive or image is corrupted after uploading to FTP and downloading back with FtpWebRequest(上傳到 FTP 并使用 FtpWebRequest 下載回來后,存檔或圖像已損壞)
本文介紹了上傳到 FTP 并使用 FtpWebRequest 下載回來后,存檔或圖像已損壞的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時送ChatGPT賬號..

我有兩種方法:

  1. 上傳文件到 FTP 服務(wù)器
  2. 從服務(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)!

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

相關(guān)文檔推薦

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 令牌授權(quán)不起作用)
ASP Core Azure Active Directory Login use roles(ASP Core Azure Active Directory 登錄使用角色)
How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何獲取守護進程或服務(wù)器到 C# ASP.NET Web API 的 Azure AD OAuth2 訪問令牌和刷新令牌) - IT屋-程序員軟件開發(fā)技
.Net Core 2.0 - Get AAD access token to use with Microsoft Graph(.Net Core 2.0 - 獲取 AAD 訪問令牌以與 Microsoft Graph 一起使用)
Azure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(異步調(diào)用時 Azure KeyVault Active Directory AcquireTokenAsync 超時)
主站蜘蛛池模板: 亚洲免费av一区 | 欧美日韩国产一区二区三区 | 91精品观看 | 国产电影精品久久 | 天天干视频| 国产 欧美 日韩 一区 | 国产高清一区二区三区 | 欧美一区二区三区在线观看 | 日本三级全黄三级三级三级口周 | 日本黄色一级视频 | 九九九视频 | 国产精品久久久久久久久久久久冷 | 日韩欧美一级片 | 久久蜜桃资源一区二区老牛 | 亚洲小视频在线播放 | 亚洲女人天堂网 | 欧美在线视频网 | 亚洲三区在线观看 | www.9191.com| 成人福利网 | 韩日免费视频 | 99精品在线观看 | 99久9| 欧美一极视频 | 天堂va在线 | 日本激情视频在线播放 | 亚洲一二三区精品 | 天天色官网 | 99精品久久久 | 9999久久| 日本福利片 | 九九热久久免费视频 | 中文字幕a√ | 欧美国产精品久久久 | 久久蜜桃av一区二区天堂 | 亚洲高清一区二区三区 | 国产一区三区视频 | 国产精品亚洲一区二区三区在线观看 | 精品1区 | av中文字幕在线观看 | 国产成人精品高清久久 |