我有一个像Json这样的对象:
var appversion = { "Id": "", "Version": "", "HelpSystemId": "", "Content": "", "FileName": "", "IsLatest": "", "LastModify": "", "AppVersionChangsets": [] };
appversion.Id = $("#Id").val();
appversion.Version = $("#Version").val();
appversion.HelpSystemId = $("#HelpSystemId").val();
appversion.IsLatest = $("#IsLatest").val();
appversion.LastModify = $("#LastModify").val();
以及我在表单中以这种方式获取的文件:
<div class="form-group">
@Html.MyLabelFor(model => model.Content, new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="file" id="_file" name="_file" />
<button onclick="abortRead();">Cancel read</button>
<div id="progress_bar"><div class="percent">0%</div></div>
</div>
</div>
我将它发送给我的控制器:
$.ajax({
url: '/AppVersion/Create2',
data: JSON.stringify(appversion),
type: 'POST',
contentType: 'application/json;',
dataType: 'json',
success: function (result) {
if (result.Success == "1") {
window.location.href = "/AppVersion/index";
}
else {
alert(result.ex);
}
}});
控制器是:
[HttpPost]
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult Create2(AppVersion appversion)
{
//Some Code
}
我的问题是我无法访问控制器中的文件。我只是得到了我的json对象。我可以将带有Json对象的文件发送到Controller吗?
if (Request.Files["file"].ContentLength > 0)
{
Stream str = Request.Files["file"].InputStream;
byte[] data = new byte[str.Length];
str.Read(data, 0, System.Convert.ToInt32(str.Length));
}
答案 0 :(得分:0)
有一个名为'ajaxfileupload'的Jquery插件,也许U可以使用它。 网址为“http://www.phpletter.com/Demo/AjaxFileUpload-Demo/”。