在 MVC 3 中获取使用 jQuery 发布的文件的 mime 类型
我需要一些帮助来获取通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定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 thename
attribute.