我想从我的上传控件中获取一个文件,并通过ajax调用将其发送到验证器操作而不进行发布。
- 用户选择多个文件
点击'validate',运行一些检查并返回验证结果。
- 如果验证成功,将显示“上传”按钮,并且可以将其发布。
JAVASCRIPT
function OnValidateSurveyFiles() {
debugger;
var SurveyId = $('#SurveyID').val();
var file_1 = document.getElementById('surveyUploadcontrol_TextBox0_Input').files[0];
var serializedFile = JSON.stringify(file_1, null, 2);
var url = '/Survey/ValidateSurveyFiles';
$.ajax({
type: 'GET',
url: url,
data: { surveyFile: serializedFile },
success: function (result) {
//debugger;
//
},
error: function (result) {
// handle errors
location.href = "/Home/"
}
});
}
CONTROLLER
public ActionResult ValidateSurveyFiles(object surveyFile)
{
try
{
do something...
}
catch(Exception ex)
{
throw ex;
}
// I would like to return a json responde with html(partial view...) and a code with the validation's result.
return View("_");
}
实际上,file_1
如下所示:
file_1: File
lastModified: 1442499299116
lastModifiedDate: Thu Sep 17 2015 16:14:59 GMT+0200 (Mitteleuropäische Sommerzeit)
name: "File.csv"
size: 1176120
type: "application/vnd.ms-excel"
webkitRelativePath: ""
__proto__: File
constructor: File()
lastModified: (...)
get lastModified: ()
lastModifiedDate: (...)
get lastModifiedDate: ()
name: (...)
get name: ()
size: (...)
type: (...)
webkitRelativePath: (...)
get webkitRelativePath: ()
__proto__: Blob
但序列化文件只是{}
任何想法我怎么能达到预期的行为?
答案 0 :(得分:0)
关注@ Stephen-Muecke链接。
查看强>
function OnValidateSurveyFiles() {
debugger;
var SurveyId = $('#SurveyID').val();
var formData = new FormData();
var fileSurvey = document.getElementById('surveyUploadcontrol_TextBox0_Input').files[0];
formData.append("FileSurvey", fileSurvey);
formData.append("SurveyID", SurveyId);
var url = '/Survey/ValidateSurveyFiles';
$.ajax({
type: 'POST',
url: url,
data: formData,
contentType: false,
processData: false,
success: function (result) {
debugger;
//
if (result.error) {
$('#containerErrorLog').show();
$('#errorLogSurvey').html(result.message);
}
else
$('#containerErrorLog').hide();
},
error: function (result) {
// handle errors
location.href = "/Home/"
}
});
}
<强>控制器强>
[HttpPost]
public ActionResult ValidateSurveyFiles()
{
try
{
int surveyId = -1;
Int32.TryParse(Request.Params["SurveyId"], out surveyId);
foreach (string file in Request.Files)
{
HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
BinaryReader reader = new BinaryReader(hpf.InputStream);
byte[] binData = reader.ReadBytes(hpf.ContentLength);
string tmpFullContent = Encoding.UTF8.GetString(binData);
if (hpf == null || hpf.ContentLength == 0)
continue;
// blablabla... validation and store list of errors into "Errors"
}
if (Errors.count > 0)
{
return Json(new
{
error = true,
message = RenderHelper.RenderViewToString(this.ControllerContext, "_surveyErrorLog", resultSurvey)
});
}
}
catch (Exception ex)
{
throw ex;
}
}
RenderHelper类
public static class RenderHelper
{
public static string RenderViewToString(ControllerContext context, string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = context.RouteData.GetRequiredString("action");
ViewDataDictionary viewData = new ViewDataDictionary(model);
using (StringWriter sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(context, viewName);
ViewContext viewContext = new ViewContext(context, viewResult.View, viewData, new TempDataDictionary(), sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
}