简体   繁体   English

Image.Write到FTP损坏?

[英]Image.Write to FTP corrupted?

I've had this code running and it's been running perfectly fine - until a few weeks back when it decided it doesn't want to work. 我已经运行了这段代码,并且运行得非常好-直到几周前,它决定它不起作用。

I'm not sure exactly why it's not working. 我不确定为什么它不起作用。

A selected image is resized and then uploaded to the server. 调整所选图像的大小,然后上载到服务器。 However, the application only uploads 4.380kb of the file - regardless what image I select. 但是,该应用程序仅上传4.380kb的文件-不管我选择什么图像。 This is regardless of what directory I save it in. 不管我将其保存在哪个目录中。

The FTP account definitely has permissions and can upload other files without issue. FTP帐户绝对具有权限,并且可以上传其他文件而不会出现问题。 Thus I think something in the code for uploading the image must be the problem? 因此,我认为代码中的某些图片上传问题一定存在吗?

UploadImage 上传图片

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uploadLocation);
FileInfo toUpload = new FileInfo(fileToUpload);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
Stream ftpStream = request.GetRequestStream();           

Image image = Image.FromFile(fileToUpload);
image = Tools.ScaleImage(image, 500, 360);
image.Save(ftpStream, ImageFormat.Jpeg);
image.Save(@"C:\Users\User\Desktop\image.jpg", ImageFormat.Jpeg);

Thinking it could be something to do with my resize code, I saved a copy of the file to the desktop - the file saved perfectly and is saved as expected. 考虑到可能与我的调整大小代码有关,我将文件的副本保存到了桌面上-文件已完美保存并按预期保存。

However, here is the code as shown: 但是,这是如下所示的代码:

ScaleImage 比例图像

public static Image ScaleImage(Image image, int maxWidth, int maxHeight)
{
    var ratioX = (double)maxWidth / image.Width;
    var ratioY = (double)maxHeight / image.Height;
    var ratio = Math.Min(ratioX, ratioY);

    var newWidth = (int)(image.Width * ratio);
    var newHeight = (int)(image.Height * ratio);

    var newImage = new Bitmap(newWidth, newHeight);
    Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
    return newImage;
}

As I say, using exactly the same FTP credentials into the same folder, I am able to use ftp stream to upload other files...Not sure why this has stopped working.... 就像我说的那样,使用完全相同的FTP凭据到同一文件夹中,我能够使用ftp流上传其他文件...不确定为什么它停止了工作....


edit: in case it helps, I've added my code to upload files (using the same ftp settings). 编辑:如果有帮助,我已经添加了代码以上传文件(使用相同的ftp设置)。 this works fine - how can I modify it to upload the Image? 效果很好-如何修改它以上传图片?

uploadfile:
FileStream file = File.OpenRead(fileToUpload);
                int length = 1024;
                byte[] buffer = new byte[length];
                int bytesRead = 0;

                totalToUpload = file.Length;
                try
                {
                    do
                    {
                        bytesRead = file.Read(buffer, 0, length);
                        ftpStream.Write(buffer, 0, bytesRead);
                        totalReadBytesCount += bytesRead;
                        var progress = totalReadBytesCount * 100.0 / totalToUpload;
                        backgroundWorker2.ReportProgress((int)progress);
                    }
                    while (bytesRead != 0);
                    file.Close();

Try to upload it using streams: 尝试使用流上传它:

MemoryStream ms = new MemoryStream();
image.Save(ms, ImageFormat.Jpeg);
byte[] imgBytes = ms.ToArray();

ftpstream.Write(imgBytes, 0, imgBytes.Length); 
ftpstream.Close(); 

You can also try to loop the writing 您也可以尝试循环写作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM