0

I have an image upload form

<% using (Html.BeginForm("PictureValidateAndSave", "UserGallery", new {}, FormMethod.Post, new { enctype = "multipart/form-data"})) { %>
    <table>
        <tr>
            <td> Album Name: </td>
            <td> <%= Html.DropDownList("albumList") %></td>
        </tr>
        <tr>
            <td> File Location: </td> 
            <td> <input type="file" name="picture" accept="image/gif, image/jpeg" /> </td>  
        <tr>
        <tr>
            <td> Picture name: </td>
            <td> <input name="pictureName" style="width: 147px;"/> </td>
        </tr>
    </table>            
    <p> <input type="submit" value="Save" /> </p>
<% } %>

That posts back to the action

public ActionResult PictureValidateAndSave(long albumList, HttpPostedFileBase picture, string pictureName)

The code works accross all browsers but Google Chrome. My IDE is Visual Studio 2k8, and I haven't figured out how to debug on Google Chrome with it, however, I am throwing error messages, and I know that for some reason under chrome, the following check doesn't pass:

string mimeType = picture.ContentType;

// Check for the correct mimeType to define the extension
switch (mimeType)
{
    case "image/pjpeg":
        mimeType = ".jpeg";
        break;
    case "image/png":
        mimeType = ".png";
        break;
    case "image/x-png":
        mimeType = ".png";
        break;
    case "image/gif":
        mimeType = ".gif";
        // Conversion to image
        Image gifImage = Image.FromStream(picture.InputStream);
        FrameDimension dimension = new FrameDimension(gifImage.FrameDimensionsList[0]);

        int frameCount = gifImage.GetFrameCount(dimension);
        // Reject if its an animated gif
        if (frameCount > 1)
        {
            return RedirectToAction("UploadPicture", new { error = 3 });
        }
        break;
    default:
        return RedirectToAction("UploadPicture", new { error = 1 });
}

So apparently, under Chrome, the HttpPostedFileBase parameter picture isn't encoded right and loses its mime type, howwever, this might not be the only problem. What is excatly wrong with the HttpPostedFileBase parameter under Chrome, and how can I fix it?

Thank you for your attention, and thanks in advance for any help.

3
  • 1
    Open your site in crome and debug it to see what value the content type is assigned. And also, isn't it easier to just check the extensions and get rid of that switch statement? Commented Feb 10, 2010 at 9:16
  • I agree with the switch statement comment. This is debugging code. I added a lot of stuff that isn't needed. Also, opening the site in chrome while the VS2k8 debugger is running, doesn't report anything. It is as if the code wasn't running. Commented Feb 10, 2010 at 22:31
  • You will be able to step through the code if you're viewing in Chrome. The VS debugger attaches to the VS Development Server, not the browser instance - that's how it's browser agnostic. I often step thru my code while viewing in ie, ff and chrome. Commented Feb 17, 2010 at 17:42

2 Answers 2

1

Just use hanselmans method, works perfect in chrome.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Al, looking at the example helped me confirm I was doing the right thing. It was a small problem with a mimetype I had not identified though. I still need to read more about the differences between jpeg and pjpeg to fully understand why Chrome is doing things differently from IE8 and FF. Chrome is indentifying all pjpegs as regular jpegs.
0

I've just debugged some of my code that uploads files to mvc and i get the exact MIME string from the HttpPostedFileBase in Firefox as i do in Chrome: "image/jpeg" and "image/gif" respectively. So i don't think there is a problem with the browser interpreting the file type.

Take a look at this answer. It has a lot of my code in it regarding uploading and typing the 'HttpPostedFileBase' data. It might get you un-stuck.

2 Comments

Thank you cottsak. Sorry for the late reply. I shelved this bug and I couldn't get back to it until now. I found my problem. Apparently that IE8 and FF interpret as image/pjpeg get interpreted in Chrome as image/jpeg, which is still valid, however I do not yet understand why there is such a difference.
I see. It seems that you might be uploading a progressive jpeg and IE wants to treat those differently. I'd just suggest using regex or similar to simplify filtering for the variation in the MIME string. More here about progressive jpeg upload in IE: stackoverflow.com/questions/115705/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.