【发布时间】:2016-02-01 13:51:37
【问题描述】:
这个 Ajax 在一个我知道有文件和属性的函数中。 什么都没有发生,我正在拖动我的文件并将其放下,但 Ajax 没有发生任何事情。当我将属性输出到页面时,我看到我在 JS 中的函数有该文件,但它没有将它发送到控制器。我不认为它工作正常,当我调试我的索引控制器时,我的参数中没有文件属性。 Ajax 没有给我成功或失败的警报消息。
我的最终目标是上传文件中的数据,以便我可以解析它。
JavaScript
function sendFile(file) {
$.ajax({
type: "POST",
url: "HomeController/Index", // the method we are calling
contentType: "application/json; charset=utf-8",
data: { filename: file.name, fileType: file.type, fileSize: file.size },
dataType: "json",
success: function(result) {
alert('Yay! It worked!');
// Or if you are returning something
alert('I returned... ' + result.WhateverIsReturning);
},
error: function(result) {
alert('Oh no :(');
}
});
Output(
"<p>File information: <strong>" + file.name +
"</strong> type: <strong>" + file.type +
"</strong> size: <strong>" + file.size +
"</strong> bytes</p>"
);
}
AJAX
$.ajax({
type: "POST",
url: "HomeController/Index", // the method we are calling
contentType: "application/json; charset=utf-8",
data: { filename: file.name, fileType: file.type, fileSize: file.size },
dataType: "json",
success: function(result) {
alert('Yay! It worked!');
// Or if you are returning something
alert('I returned... ' + result.WhateverIsReturning);
},
error: function(result) {
alert('Oh no :(');
}
});
C#
public IActionResult Index(string fileName, string fileType, int fileSize)
{
ViewBag.Environments = _configHelper.GetEnvironments();
var model = new HomeViewModel { Environment = "DEV" };
return View(model);
}
CSHTML
<div class="form-group col-md-12">
<label>Company Ids:</label>
<div id="filedrag"><textarea class="form-control" rows="5" id="textAreaCompanyIDs" placeholder="Drop Files Here"></textarea>
</div>
</div>
【问题讨论】:
-
如果您尝试发送实际文件,您需要传递的不仅仅是名称、大小等属性,您还必须发送实际文件对象
-
谢谢,我需要做的就是获取文件中的内容,以便解析数据。
标签: javascript c# asp.net ajax asp.net-mvc