【发布时间】:2015-03-06 22:50:16
【问题描述】:
我一直在努力让这个 blueimp 的文件上传代码正常工作。有很多可用的示例,但没有一个使用 MVC razor。
我的 bundleconfig.cs 和 _layout 共享视图中包含所有必要的 css 和 js 文件。
有没有人做过类似于我想要完成的事情?
click here to view basic file upload
查看
<!-- The fileinput-button span is used to style the file input field as button -->
<form class="form-horizontal" method="post"
action="/Account/UserProfilePic" id="ProfilePicForm"
name="ProfilePicForm" enctype="multipart/form-data">
<span class="btn btn-success fileinput-button">
<i class="glyphicon glyphicon-plus"></i>
<span>Select files...</span>
<!-- The file input field used as target for the file upload widget -->
<input id="fileupload" type="file" name="files[]" multiple>
</span>
<br>
<br>
<!-- The global progress bar -->
<div id="progress" class="progress">
<div class="progress-bar progress-bar-success"></div>
</div>
<!-- The container for the uploaded files -->
<div id="files" class="files"></div>
</form>
<br>
$(function () {
'use strict';
var url = '/Account/UploadProfilePic';
$('#fileupload').fileupload({
url: url,
dataType: 'json',
done: function (e, data) {
$.each(data.result.files, function (index, file) {
$('<p/>').text(file.name).appendTo('#files');
});
},
progressall: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
$('#progress .progress-bar').css(
'width',
progress + '%'
);
}
}).prop('disabled', !$.support.fileInput)
.parent().addClass($.support.fileInput ? undefined : 'disabled');
});
控制器
[HttpPost]
public JsonResult UserProfilePic(HttpPostedFileBase file)
{
string UserId = System.Web.HttpContext.Current.User.Identity.GetUserId();
var guid = Guid.NewGuid();
string relativePath = @"~/Content/ProfilePics/" + guid + file.FileName;
string path = Server.MapPath(relativePath);
file.SaveAs(path);
UserProfileViewModel model = new UserProfileViewModel();
model.ProfilePicURL = relativePath;
model.UserId = UserId;
userRepository.UploadPic(relativePath, UserId);
return Json(new { name = file.FileName });
}
【问题讨论】:
标签: jquery json razor file-upload asp.net-mvc-5