我有一个问题发布csv文件到我的后台操作方法。我在Controller PropertyController中有一个名为UploadPropertyCSV的操作方法,用于处理文件并将其添加到数据库中,但由于某种原因,当我单击提交时,文件从不碰到操作方法,只是刷新页面并不执行任何操作。我的前端代码如下所示:无法发布CSV文件上传到控制器操作
<form id="Form2" method ="post" name="Form2" enctype="multipart/form-data">
<input type="file" name="file" id="file" multiple />
<input type="submit" value="Upload" />
</form>
<script>
$(function() {
$("#Form2").submit(function (event) {
var formData = new FormData($("#Form2").get(0));
$.ajax({
url: "Property/UploadPropertyCSV",
type: 'POST',
dataType: 'json',
data: formData,
processData: false,
contentType: false,
success: function (result) {
alert(result.Message)
}
})
});
});
</script>
我的操作方法返回要显示在警报按照以下方法JSON消息:
public ActionResult UploadPropertyCSV(HttpPostedFileBase file)
{
List<PropertyModel> properties = new List<PropertyModel>();
TestD dbContext = new TestDB();
foreach (string requestFiles in Request.Files)
{
if (file != null && file.ContentLength > 0 && file.FileName.EndsWith(".csv"))
{
using(StreamReader str = new StreamReader(file.InputStream))
{
using(CsvHelper.CsvReader theReader = new CsvHelper.CsvReader(str))
{
int rowCount = 0;
while (theReader.Read())
{
try {
rowCount++;
//theReader.Configuration.HasHeaderRecord = true;
//if (theReader.IsRecordEmpty())
//{
// RIMUtil.LogError("Empty Row: " + rowCount);
//}
RIMUtil.PropertyUploadCSVRowHelper row = new RIMUtil.PropertyUploadCSVRowHelper()
{
UnitNumber = theReader.GetField(0),
StreetNumber = theReader.GetField(1),
StreetName = theReader.GetField(2),
City = theReader.GetField(3),
PostalCode = theReader.GetField(4),
Country = theReader.GetField(5),
NumberOfBedrooms = theReader.GetField(6),
NumberOfBathrooms = theReader.GetField(7),
NumberOfCarSpaces = theReader.GetField(8),
KeyNumber = theReader.GetField(9),
ExternalPropertyID = theReader.GetField(10),// Renamed to extPropertyID
IsVacant = theReader.GetField(11),
PropertyManagerId = theReader.GetField(12),
BuildingName = theReader.GetField(13)
};
// Missing field checks
List<string> missingFields = new List<string>();
if (missingFields.Count > 0)
{
RIMUtil.LogError("Row: " + rowCount + "Has a missing mandatory field");
return Json(new { Success = false, Message = "Error: Missing mandatory field!" });
}
else
{
// Invalid field checks
if (invalidFields.Count > 0)
{
return Json(new { Success = false, Message = "Error: An invalid entry exists!" });
}
else
{
Property property = new Property();
//object creation stuff here
dbContext.Properties.Add(property);
dbContext.SaveChanges();
}
}
}catch(Exception e)
{
return Json(new { Success = true, Message = "CSV is formatted incorrectly" });
}
}
return Json(new { Success = true, Message = "Success!" });
}
}
}
return Json(new { Success = false, Message = "Empty file or wrong file format!" });
}
return Json(new { Success = false, Message = "Error Occured!" });
}
有没有更好的办法,我可以写这个功能? JSON返回的消息警报也没有响应。
如果有人有更好的方式来写我的前端代码或解决方案的任何建议,将不胜感激!提前
不相关,但你没有取消默认提交 - 你做了一个正常的提交和ajax调用($(“#Form2”)中的最后一行)submit(function(event){....}应该是'return false;'该方法应该是'IEnumerable文件'然后你可以循环参数而不是使用'Request.Files' –
你在浏览器控制台中得到什么错误信息? –
谢谢提示@StephenMuecke 我收到错误404 - 找不到资源 – Mark