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

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

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

問題描述

限時送ChatGPT賬號..

我有兩種方法:

  1. 上傳文件到 FTP 服務器
  2. 從服務器下載文件.

一切都與文本或 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 下載回來后,存檔或圖像已損壞的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 令牌授權不起作用)
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(如何獲取守護進程或服務器到 C# ASP.NET Web API 的 Azure AD OAuth2 訪問令牌和刷新令牌) - IT屋-程序員軟件開發技
.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(異步調用時 Azure KeyVault Active Directory AcquireTokenAsync 超時)
主站蜘蛛池模板: 日韩高清在线观看 | 欧美精品二区 | 91视频网址 | 国产a视频| a视频在线观看 | 亚洲精品久久久久久首妖 | 亚洲精品久久久 | 欧美专区在线观看 | 国产精品毛片无码 | 777毛片| 911网站大全在线观看 | 欧美日韩一区二区在线观看 | 欧美成人二区 | 中文字幕一二三 | 久久久久久国产一区二区三区 | h小视频 | 精品久久一区二区 | 国产精品一区二区三区四区五区 | 日本久久黄色 | 久久久免费少妇高潮毛片 | 亚洲成av人片在线观看无码 | 久久久无码精品亚洲日韩按摩 | 久草成人| 免费黄色大片 | 国产高清在线精品一区二区三区 | 99免费在线 | 人人九九精 | 日韩中文字幕免费在线观看 | 欧美成人激情视频 | 久久久久国产 | 欧美日韩精品一区二区天天拍 | 在线观看视频h | 色性av| 99色综合| 国产精品一区在线 | a级免费观看视频 | 在线欧美亚洲 | 婷婷综合网 | 91视频一区二区三区 | 国产亚洲一区二区精品 | 亚洲免费在线视频 |