在 MVC 3 中获取使用 jQuery 发布的文件的 mime 类型

发布于 2024-11-04 11:50:26 字数 2418 浏览 1 评论 0原文

我需要一些帮助来获取通过 jQuery 从 MVC 3 中的视图上传的文件的 ContentType。

下面是视图中处理帖子的代码:

<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>
}

这是控制器操作中我需要 mime 类型的部分[IE 部分工作正常]:

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;
}

每当使用 IE 以外的浏览器时,Request.Files 不包含任何键...有什么办法解决这个问题吗? mime 类型对于数据库来说是必须的。

非常感谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

在你怀里撒娇 2024-11-11 11:50:26

您确定post变量名称“qqfile”正确吗?检查 jQuery 调用生成的 标记,并验证它的 name 属性值是否正确。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文