我有一個問題發佈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