ASP.NET图像上载参数无效。例外

时间:2011-01-05 13:50:14

标签: c# asp.net image upload uploadify

我只是尝试使用来自jquery uploadify

的发布流将文件保存到磁盘

我也得到参数无效。

添加到错误消息,以便我可以告诉它在生产中爆炸的地方我看到它爆炸了:

   var postedBitmap = new Bitmap(postedFileStream)

任何帮助都将非常感激

public string SaveImageFile(Stream postedFileStream, string fileDirectory, string fileName, int imageWidth, int imageHeight)
{
    string result = "";
    string fullFilePath = Path.Combine(fileDirectory, fileName);
    string exhelp = "";

    if (!File.Exists(fullFilePath))
    {
        try
        {
            using (var postedBitmap = new Bitmap(postedFileStream))
            {
                exhelp += "got past bmp creation" + fullFilePath;

                using (var imageToSave = ImageHandler.ResizeImage(postedBitmap, imageWidth, imageHeight))
                {
                    exhelp += "got past resize";

                    if (!Directory.Exists(fileDirectory))
                    {
                        Directory.CreateDirectory(fileDirectory);
                    }

                    result = "Success";
                    postedBitmap.Dispose();
                    imageToSave.Save(fullFilePath, GetImageFormatForFile(fileName));
                }

                exhelp += "got past save";
            }
        }
        catch (Exception ex)
        {
            result = "Save Image File Failed " + ex.Message + ex.StackTrace;
            Global.SendExceptionEmail("Save Image File Failed " + exhelp, ex);
        }
    }

    return result;
}

2 个答案:

答案 0 :(得分:0)

您的流类型似乎对Bitmap对象无效,尝试将流复制到MemoryStream,并将MemoryStream作为Bitmap的参数

并删除第二次处理@Aliostad mentoined

类似这样的事情

public string SaveImageFile(Stream postedFileStream, string fileDirectory, string fileName, int imageWidth, int imageHeight)
        {
            string result = "";
            string fullFilePath = Path.Combine(fileDirectory, fileName);
            string exhelp = "";
            if (!File.Exists(fullFilePath))
            {
                try
                {
                    using(var memoryStream = new MemoryStream())
                    {
                       postedFileStream.CopyTo(memoryStream);
                       memoryStream.Position = 0;
                       using (var postedBitmap = new Bitmap(memoryStream))
                       {
                         .........
                        }

                    }
                }

                catch (Exception ex)

                {

                    result = "Save Image File Failed " + ex.Message + ex.StackTrace;

                    Global.SendExceptionEmail("Save Image File Failed " + exhelp, ex);

                }

            }



            return result;

        }

答案 1 :(得分:0)

使用图像转换器从字节数组加载:

ImageConverter imageConverter = new System.Drawing.ImageConverter();
Image image = imageConverter.ConvertFrom(byteArray) as Image;

http://forums.asp.net/t/1109959.aspx