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

查看:497
本文介绍了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..!

我们已经尝试重新启动服务器,但没有任何变化。
同样的code运行我的机器上的罚款。 (听说?:)之前)

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."

如何解决此问题?

推荐答案

我会确保流被定位在启动:

I would make sure the stream is positioned at the start:

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应该没有总体差异是否使用图像直接读取文件流,或者如果您不是事实,使用中间的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天全站免登陆