久久久久久久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 超時)
主站蜘蛛池模板: 天天综合久久 | 国产欧美日韩一区 | 日韩精品二区 | 日本亚洲天堂 | 日本毛片在线观看 | 成人在线播放视频 | 国产中文字幕在线播放 | 黄色一级小说 | 欧美成人免费在线视频 | 男女免费视频 | 黄色草逼视频 | 亚洲一区二区免费 | 在线国产一区 | 影音先锋在线观看视频 | 黄色片视频在线观看 | aaa黄色| www.草逼| 国产精品一级 | av毛片网站 | 伊人av综合 | 一级片在线 | 日韩中文字幕视频 | 日韩精品中文字幕在线观看 | 久久人体 | 人人射人人 | 亚洲精品一区二区三区精华液 | 激情91 | 国产亚洲精品成人av久久ww | 亚洲黄色三级 | 免费网站黄 | 久久国产精品视频 | 欲色av| 色婷婷导航 | 成人午夜网 | 久草手机在线 | 亚洲色网址| 中文字幕免费在线 | 日韩av在线免费看 | 亚洲精品在线视频 | 国产成人免费在线观看 | 日本在线看片 |