Image.FromStream(PostedFile.InputStream) 失败.(参数无效.)(AsyncFileUpload)) [英] Image.FromStream(PostedFile.InputStream) Fails. (Parameter is not valid.) (AsyncFileUpload))

查看:35
本文介绍了Image.FromStream(PostedFile.InputStream) 失败.(参数无效.)(AsyncFileUpload))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AsyncFileUpload(AJAX 工具包)上传图片.我有一个处理图像大小调整的按钮.这已经有一段时间了,但现在不行了......

I'm using an AsyncFileUpload (AJAX Toolkit) to upload images. I have a Button which handle the image resizing. This have worked fine for some time, but not anymore...

protected void BtnUploadImage_Click(object sender, EventArgs e)
{
    var imageFileNameRegEx = new Regex(@"(.*?).(jpg|jpeg|png|gif)$", 
        RegexOptions.IgnoreCase);
    if (!AsyncFileUpload1.HasFile || 
        !imageFileNameRegEx.IsMatch(AsyncFileUpload1.FileName))
    {
        AsyncFileUpload1.FailedValidation = true;
        ErrorLabel.Visible = true;
        return;
    }
    ErrorLabel.Visible = false;

    var file = AsyncFileUpload1.PostedFile.InputStream;

    var img = Image.FromStream(file, false, false);

...
}

另一件我觉得很奇怪的事情:如果我尝试使用小于 80kb 的图像,它会起作用..!

Another thing which I find weird: If I try a image which is smaller than 80kb it works..!

我们已尝试重新启动服务器,但没有任何变化.相同的代码在我的机器上运行良好.(之前听说过??:))

We have tried to restart the server, but no change. Same code runs fine on my machine. (heard that before ?? :) )

我也尝试将文件保存在服务器上,然后通过 Image.FromFile() 获取文件,但随后出现无法访问已关闭的文件".

I also tried to save the file on the server, then to get the file trough Image.FromFile(), but then I get "Cannot access a closed file."

如何解决这个问题?

推荐答案

我会确保流位于开头:

var file = AsyncFileUpload1.FileContent;
file.Seek(0, SeekOrigin.Begin);

var img = Image.FromFile(file);

要检查的第二件事:requestLengthDiskThreshold 设置.除非指定,否则此设置的默认值为 ... 是的,80 KB.

Second thing to check: the requestLengthDiskThreshold setting. Unless specified this setting has a default of ... yes, 80 KB.

注意: imo 无论是使用 Image 直接读取文件流,还是使用中间的 MemoryStream(除了在后一种情况下您实际加载整个文件两次进入内存).无论哪种方式,都会读取原始文件流,因此流位置、CAS 权限、文件权限等仍然适用.

Note: imo there should be no overall difference whether you use Image to read the file stream directly or if you use an intermediate MemoryStream (other than the fact that in the latter case you actually loads the entire file into memory twice). Either way the original file stream will be read from, thus stream position, CAS rights, file permissions, etc still applies.

注意 2: 是的,请务必确保正确处理这些资源 :)

Note2: and yes, by all means make sure those resources are disposed properly :)

这篇关于Image.FromStream(PostedFile.InputStream) 失败.(参数无效.)(AsyncFileUpload))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆