【问题标题】:Image gets corrupted after FileUpload with FtpWebRequest使用 FtpWebRequest 上传文件后图像损坏
【发布时间】:2019-11-04 23:31:45
【问题描述】:

我有一个上传图片的代码。我使用await/async。例如,代码会上传 .txt 文件和 .jpg 文件。

但是,在上传 .jpg 图片时。它确实被上传。但是,当我从服务器手动将其下载到我的桌​​面(使用 FileZilla)时,我无法在绘图中打开该文件。我收到此错误:

Paint 无法读取此文件。这不是一个有效的位图文件,或其格式目前不受支持

其他信息:
原始文件大小:109 309 字节 上传到服务器时:109 308 字节

 void uploadimage()
        {
            String sourceimage = "C:/ESD/image_2.jpg";
            Task<bool> task = FtpUploadFile(sourceimage);
            if (task.IsFaulted == false)
            {
                MessageBox.Show(task.Result.ToString());
            }
        }
        private Task closeRequestStreamAsync(Stream requestStream) { return Task.Run(() => { requestStream.Close(); }); }
        public async Task<bool> FtpUploadFile(string filename)
        {
            //if exception occurs we want to be able to close these
            FtpWebResponse response = null;
            FtpWebRequest request = null;
            FileStream sourceStream = null;
            Stream requestStream = null;
            try
            {
                bool isimage = false; String ext = Path.GetExtension(filename);
                if (ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".gif" || ext == ".bmp") { isimage = true; }

                request = (FtpWebRequest)WebRequest.Create("ftp://someurl.com/Folder1/test1.jpg");
                request.UsePassive = true;
                if (isimage == true) { request.UseBinary = true; } //for images
                if (isimage == false) { request.UseBinary = false; } //for text
                request.KeepAlive = true; //keep the connection open
                request.Method = WebRequestMethods.Ftp.UploadFile;
                request.ConnectionGroupName = "Group1";
                request.ServicePoint.ConnectionLimit = 4;

                //These are the credentials.
                request.Credentials = new NetworkCredential("username", "password");


                sourceStream = File.OpenRead(filename);
                byte[] buffer = new byte[sourceStream.Length];
                await sourceStream.ReadAsync(buffer, 0, buffer.Length);
                sourceStream.Close();

                requestStream = await request.GetRequestStreamAsync();
                await requestStream.WriteAsync(buffer, 0, buffer.Length);
                //MPM  This is the call that takes the time     
                await closeRequestStreamAsync(requestStream);

                //response = (FtpWebResponse)request.GetResponse();
                WebResponse responseWeb = await request.GetResponseAsync();
                response = (FtpWebResponse)responseWeb;
                if (response.StatusDescription.Contains("226"))
                {
                    //This means that we successfully have uploaded the file!
                }
                response.Close();
                return true;
            }
            catch (Exception ex)
            {
                string errMSG = string.Format("Upload File failed, exception: {0}", ex.Message);
                return false;
            }
        }

【问题讨论】:

  • "它确实被上传了。"你怎么检查这个?您是否在服务器上打开文件并显示正确的图像?您是否在检查正确的文件大小?您只是看到代码“有效”吗?
  • response.StatusDescription.Contains("226") 告诉它已上传。然后我在 FileZilla 中目视检查该文件是否存在。但是文件以某种方式损坏。例如,我无法从服务器打开它或将其下载到桌面并在绘图中打开它。

标签: file-upload async-await ftpwebrequest


【解决方案1】:

我确实忘记将其添加到我的代码中,因此图像确实使用二进制图像。现在可以了:

 if (isimage == true) { request.UseBinary = true; } //for images
 if (isimage == false) { request.UseBinary = false; } //for text

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多