开发者

In ASP.NET, What could cause image manipulation to fail when using Fx or chrome, but succeed when using IE?

I am in a rather unusual pickle. I am modifying an image uploader and I thought I had it working. It needed to:

  1. take a file from the client and upload it to server.
  2. If file is an image, perform resizing operations on it.
  3. If file is an image, create a thumbnail.

What I have works great when uploading images with Internet Explorer 8. But, when I upload images using Chrome, or Firefox3.+, the image gets uploaded but steps 2 and 3 are not performed. I don't get any server errors or anything. As 2 and 3 are steps that are performed on the server I have no idea how a change in browser could effect them.

I'm not sure if it has anything to do with my checking for whether the file is an image or not. But, for the sake of being thorough, here's the code I use:

try
{
    string Filename = FileSystemUtilities.Cleanup开发者_运维技巧Filename(Path.GetFileName(hpf.FileName));
    Filename = hpf.FileName;
    string FileToSave = DestDir + Path.DirectorySeparatorChar + Path.GetFileName(Filename);
    hpf.SaveAs(FileToSave);
    bool IsImageFileType = ImageUtilities.IsImage(Filename, imageExtensions);

    // below does not seem to execute when using non ie browser
    // everything is smooth sailing when using ie.
    if (IsImageFileType)
    {
        ImageUtilities.ResizeImageIfNecessary(FileToSave, mainMaxWidth, mainMaxHeight);
        ImageUtilities.CreateThumbnail(FileToSave, thumbMaxWidth, thumbMaxHeight);
    }

    ValidOperation++;
    sb.AppendFormat("{0} uploaded successfully<br/>", Filename);
}

Any thoughts? Why would server side code behave differently based on browser?

Edit: ImageUtilities.IsImage()

    public static bool IsImage(string file, string[] imageExtensions)
    {
        file = Path.GetFullPath(file);

        if (File.Exists(file))
        {
            string CurrentFileExtension = Path.GetExtension(file);
            return imageExtensions.Count(x => x == CurrentFileExtension) > 0 ? true : false;
        }
        else
        {
            return false; //file doesn't exist
        }
    }


This difference would be caused by a difference in the filename sent by the browsers.
For example, some browsers include the full path.

Your ImageUtilities.IsImage function can't handle the filename sent by non-IE browsers.

EDIT: Your function is very wrong.
Change it to

return imageExtensions.Contains(Path.GetExtension(file), 
                                StringComparer.OrdinalIgnoreCase);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜