开发者

Get mime type of file posted with jQuery in MVC 3

I need some help to get the ContentType of a file that is uploaded via jQuery from a view in MVC 3.

Below is the code in the view that handles the post:

<div>
    <label for="username">Username</label><br/>
    <input type="text" name="username" id="username" value="" /><br/>
    <img id="thumb" /><br/>
    <a id="ajaxUpload" href="#">Upload Image</a><br/>
    <div class="preview"></div>
</div>

@section JavascriptContent {
<script type="text/javascript" src="/content/js/jquery/fileuploader.js"></script>

<script type="text/javascript">
//<![CDATA[
    var thumb = $('img#thumb'); 

    var uploader = new qq.FileUploaderBasic({
        button: $('#ajaxUpload')[0],
        enctype: 'multipart/form-data',
        action: '@Url.Action("someAction", "someController")',
        params: { },
        name: 'imageFile',
        allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'],  
        //each file size limit in bytes
        //this option isnt supported in all browsers
        sizeLimit: 4096000, // max size   
        minSizeLimit: 2048, // min size
        multiple: false,
        debug: true,
        onSubmit: function(file, extension) {
            this.params = { boardId: $('#someGuid').val(), username: $('#username').val() };
            $('div.preview').addClass('loading'); // Loading...
        },
        onComplete: function开发者_运维问答(id, filename, response) {
            thumb.load(function(){
                $('div.preview').removeClass('loading');
                thumb.unbind();
            });
            if (response.Success){
                thumb.attr('src', response.FilePath);
            } else {
                alert(response.Message); //json response that contains a message and some other info
            }
        }
    });
//]]>   
</script>
}

and here is the part of the controller action where i need the mime type [the IE part works fine]:

var stream = Request.InputStream;
if (String.IsNullOrWhiteSpace(Request["qqfile"]))
{
    // IE
    HttpPostedFileBase postedFile = Request.Files[0];
    stream = postedFile.InputStream;
    fileContentType = postedFile.ContentType;
    fileName = System.IO.Path.GetFileName(postedFile.FileName);
}
else
{
    //Mozilla / Safari
    //TODO: this is where i need to get the mime type
    fileName = qqfile;
}

Whenever using a browser other than IE Request.Files contains no keys... is there any way around this? The mime type is a MUST for the database.

Many thanks!


Are you sure that the post variable name "qqfile" is correct? Check the <input> tag generated by the jQuery call and verify that it has the correct value of the name attribute.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜