上传文件后,我将它保存在磁盘上之前,我正在进行一些验证。MVC 3&Uploadify - 如何显示自定义错误信息?
如果失败了,我想向用户显示自定义错误信息。 我在这里找到了一些类似的东西Uploadify: show error message from HTTP response
如何在MVC 3中做到这一点?
上传文件后,我将它保存在磁盘上之前,我正在进行一些验证。MVC 3&Uploadify - 如何显示自定义错误信息?
如果失败了,我想向用户显示自定义错误信息。 我在这里找到了一些类似的东西Uploadify: show error message from HTTP response
如何在MVC 3中做到这一点?
托尼,
直接从我的工作,现在AB MVC3应用摘自:
<script type="text/javascript">
function initupLoadify() {
$("#fileInput").uploadify({
uploader: '@Url.Content("~/scripts/swf/uploadify.swf")',
script: '@Url.Action("Upload", "Home")',
cancelImg: '@Url.Content("~/Content/cancel.png")',
auto: true,
sizeLimit: 5500000,
fileDataName: 'fileData',
//scriptData: { 'propertyId': $("#PropertyID").val() },
buttonText: 'Add Schedule',
wmode: 'transparent',
//buttonImg: '@Url.Content("~/Content/button.png")', // make nice gradient image for button
onComplete: function (event, queueId, fileObj, response) {
$("#msg").html(response);
// do something
setTimeout(2000, window.location = '@Url.Action("Index")' + '?id=' + response);
return true;
},
onCancel: function (evt, queueId, fileObj, data) {
$("#msg").html("<br />Operation cancelled");
return true;
},
onOpen: function (evt, queueId, fileObj) {
$("#msg").html("<br />Upload in progress");
return true;
},
onError: function (event, queueId, fileObj, errorObj) {
$("#msg").html(fileObj.name + " was not uploaded ");
if (errorObj.status == 404)
$("#msg").html("Could not find upload script. Use a path relative to: " + "<?= getcwd() ?>");
else if (errorObj.type === "HTTP")
$("#msg").html("error " + errorObj.type + ": " + errorObj.status);
else if (errorObj.type === "File Size")
$("#msg").html(fileObj.name + " " + errorObj.type + " Limit: " + errorObj.info/1000 + " KB");
else
$("#msg").html("error " + errorObj.type + ": " + errorObj.text);
}
});
};
</script>
希望这有助于
这里是一个MVC 3 example。
如果您使用的版本是3.0+,使用onUploadComplete而不是onComplete(实现它们的参数不同)。
我见过类似的代码,但它只显示文件的数据。但是如果我想要显示消息在服务器上发生了什么? – Tony 2012-07-20 11:03:50
另外,'errorObj.text'返回undefined – Tony 2012-07-20 11:05:15
tony - 我明白你说的话(控制器/服务层代码,tho没有意识到这个问题)。我还没有处理这个问题,但是你现在有了我的'研究'头脑,所以会有一个挖掘 – 2012-07-20 11:07:02