图像上传到FTP问题

时间:2016-09-27 21:22:44

标签: c# .net asp.net-mvc

我使用以下代码将图像上传到我的FTP服务器,但是我遇到了问题。如果我在上传之前使用代码检查图像的尺寸(高度和宽度),那么在FTP服务器中创建了.png文件,但它是空的(或格式无效),如果我删除代码来检查尺寸,那么图像上传正确。有没有任何想法?

    public ActionResult UploadFile(int type, HttpPostedFileBase imagefile)
    {
        //check image height and width
        using (System.Drawing.Image image = System.Drawing.Image.FromStream(imagefile.InputStream, true, true))
        {
            if (image.Width > 160 || image.Height > 160)
            {
                //do something here
            }
        }//end check image height and width

        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftpPath" + "/" + imagefile.FileName);
        request.Credentials = new NetworkCredential("ftpUserName", "ftpPassword");

        request.Method = WebRequestMethods.Ftp.UploadFile;
        var sourceStream = imagefile.InputStream;
        Stream requestStream = request.GetRequestStream();
        request.ContentLength = sourceStream.Length;
        int BUFFER_SIZE = imagefile.ContentLength;
        byte[] buffer = new byte[BUFFER_SIZE];
        int bytesRead = sourceStream.Read(buffer, 0, BUFFER_SIZE);
        do
        {
            requestStream.Write(buffer, 0, bytesRead);
            bytesRead = sourceStream.Read(buffer, 0, BUFFER_SIZE);
        } while (bytesRead > 0);
        sourceStream.Close();
        requestStream.Close();
        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        response.Close();
}

2 个答案:

答案 0 :(得分:0)

如上所述,当您将其加载到Image时,您正在阅读所有流。但是,我不认为您可以在NetworkStream(您的InputStream)上重置位置(即“Seek”)。一旦你阅读它,它就消失了。

您可以做的一件事是创建MemoryStream并使用Stream.CopyTo将内容复制到其中。然后,您可以随意做任何事情,包括将位置重置为0以“再次读取”。

//example of resetting a stream named "s"
s.Position = 0;

答案 1 :(得分:0)

感谢各位回复他们非常有帮助,但我通过对我的代码稍作修改来修复此问题,我在阅读输入Stream后检查了图像尺寸。可能有人觉得这很有帮助。

public string ftpUpload(HttpPostedFileBase imagefile, string filename)
        {
            var sourceStream = imagefile.InputStream;
            int BUFFER_SIZE = imagefile.ContentLength;
            byte[] buffer = new byte[BUFFER_SIZE];
            int bytesRead = sourceStream.Read(buffer, 0, BUFFER_SIZE);

            if (!CheckLogoDimension(sourceStream))
            {
                sourceStream.Close();
                return "error";
            }

            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpRootPath + "/" + filename);
            request.Credentials = new NetworkCredential(ftpUserName, ftpPassword);

            request.Method = WebRequestMethods.Ftp.UploadFile;

            Stream requestStream = request.GetRequestStream();
            request.ContentLength = sourceStream.Length;
            do
            {
                requestStream.Write(buffer, 0, bytesRead);
                bytesRead = sourceStream.Read(buffer, 0, BUFFER_SIZE);
            } while (bytesRead > 0);
            sourceStream.Close();
            requestStream.Close();
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            response.Close();

            return "Success";
        }