AjaxFileUpload 插件是一个很简洁很好用的上传文件的插件,可以实现异步上传功能,但是在 ASP.NET MVC中使用时,会出现上传图片后不能正确的显示的问题,经过仔细排查,终于找到原因,解决方法如下:

 uploadHttpData: function (r, type) {

        var data = !type;
// var data = r;
data = type == "xml" || data ? r.responseXML : r.responseText; // If the type is "script", eval it in global context
if (type == "script")
jQuery.globalEval(data);
// Get the JavaScript object, if JSON is used.
if (type == "json")
{
// eval("data = " + data);
/*++++++++++++++以下为新增代码++++++++++++++++++++++++++*/
var datastr = r.responseText;
var newdatastr = datastr.replace("<pre>", "").replace("</pre>", "");
data = JSON.parse(newdatastr); /*+++++++++++++++以上为新增代码++++++++++++++++++++++++++*/ }
// evaluate scripts within html
if (type == "html")
jQuery("<div>").html(data).evalScripts(); return data;
},

通用封装后的方法:

function fileUpload(requrl,elementid,modelimgid) {
$("#loading").ajaxStart(function () {
$(this).show();
}).ajaxComplete(function () {
$(this).hide();
}); $.ajaxFileUpload(
{
url: requrl,
secureuri: false,
fileElementId: elementid,
dataType: 'json',
data: { name: 'logan', id: 'id' },
success: function (data, status) {
if (typeof (data.Type) !== 'undefined') {
if (data.Type !== 'success') {
alert(data.Data);
} else {
//alert("aaa");
$('#imgshow').show();
$('#imgshow').append("<img src='" + data.Data + "' width='100' height='60' />");
$("#" + modelimgid).val(data.Data);
}
}
},
error: function (data, status, e) {
alert(e);
}
}
)
return false;
}

View 视图中调用:

       function ajaxFileUpload() {
var url = "/SkinCategory/UploadFile";
var elementid = "fileToUpload";
var filepath = $("#fileToUpload").val();
var file = $("#fileToUpload");
//validateImage(file);
var modelimgid = "Icon";
return fileUpload(url, elementid, modelimgid);
}
        <div class="controls">
<label> 自定义图标 </label> @if (Model != null)
{
if (!string.IsNullOrWhiteSpace(Model.Icon))
{
<span> 当前使用的图标</span>
<img src="@Model.Icon" class="imghref" alt="自定义图标" />
<input type="file" name="fileToUpload" id="fileToUpload" />
}
}
else
{
<span>请上传图标</span>
<input type="file" name="fileToUpload" id="fileToUpload" />
} @Html.HiddenFor(p => p.Icon)
<button id="buttonUpload" onclick=" return ajaxFileUpload();">
上传
</button> <img id="loading" src="/Content/img/loading.gif" style="display: none;" />
<div id="imgshow" style="display: none;">
</div> </div>

Control 代码:

        public ActionResult UploadFile()
{
string savePath = ConfigHelper.GetProConfigString("skinCategory");
HttpPostedFileBase pfile = Request.Files[];
bool success = FileHelper.UploadFile(pfile, ref savePath);
Message msg = Message.UpLoadFileMessage(success);
msg.Data = savePath;
return Json(msg, JsonRequestBehavior.AllowGet); }

效果:

ASP.NET MVC 中使用 AjaxFileUpload 插件时,上传图片后不能显示(预览)的更多相关文章

  1. 在Asp.Net MVC中使用NPOI插件实现对Excel的操作(导入,导出,合并单元格,设置样式,输入公式)

    前言 NPOI 是 POI 项目的.NET版本,它不使用 Office COM 组件,不需要安装 Microsoft Office,目前支持 Office 2003 和 2007 版本. 1.整个Ex ...

  2. 在ASP.NET MVC中使用JQ插件datatable

    1. Models public class Citys { public int Id { get; set; } public string CityName { get; set; } publ ...

  3. 如何在 ASP.NET MVC 中集成 AngularJS(2)

    在如何在 ASP.NET MVC 中集成 AngularJS(1)中,我们介绍了 ASP.NET MVC 捆绑和压缩.应用程序版本自动刷新和工程构建等内容. 下面介绍如何在 ASP.NET MVC 中 ...

  4. 如何在 ASP.NET MVC 中集成 AngularJS(1)

    介绍 当涉及到计算机软件的开发时,我想运用所有的最新技术.例如,前端使用最新的 JavaScript 技术,服务器端使用最新的基于 REST 的 Web API 服务.另外,还有最新的数据库技术.最新 ...

  5. Asp.net mvc 中View 的呈现(二)

    [toc] 上一节介绍了 Asp.net mvc 中除 ViewResult 外的所有的 ActionResult,这一节介绍 ViewResult. ViewResultBase ViewResul ...

  6. [Asp.net mvc]Asp.net mvc 中使用LocalStorage

    目前使用比较多的本地存储方案有比如Flash SharedObject.Google Gears.Cookie.LocalStorage.User Data.Open Database等方案.综合比较 ...

  7. C# 动态生成word文档 [C#学习笔记3]关于Main(string[ ] args)中args命令行参数 实现DataTables搜索框查询结果高亮显示 二维码神器QRCoder Asp.net MVC 中 CodeFirst 开发模式实例

    C# 动态生成word文档 本文以一个简单的小例子,简述利用C#语言开发word表格相关的知识,仅供学习分享使用,如有不足之处,还请指正. 在工程中引用word的动态库 在项目中,点击项目名称右键-- ...

  8. ASP.NET MVC中controller和view相互传值的方式

    ASP.NET MVC中Controller向view传值的方式: ViewBag.ViewData.TempData 单个值的传递 Json 匿名类型 ExpandoObject Cookie Vi ...

  9. 如何在 ASP.NET MVC 中集成 AngularJS(3)

    今天来为大家介绍如何在 ASP.NET MVC 中集成 AngularJS 的最后一部分内容. 调试路由表 - HTML 缓存清除 就在我以为示例应用程序完成之后,我意识到,我必须提供两个版本的路由表 ...

随机推荐

  1. postgresql数据库文件目录

    不同的发行版位置不同 查看进程 ps auxw | grep postgres | grep -- -D 找到默认的目录 /usr/lib/postgresql/9.4/bin/postgres -D ...

  2. httpd安装

    1.软件准备 http://apache.fayea.com/apache-mirror//apr/apr-1.5.1.tar.gz http://apache.fayea.com/apache-mi ...

  3. 45. Jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  4. PUA

    约会技巧 kino技巧 被拒绝的应对方法 (1)一般约会7个小时后,就能带女生回家 (2)点菜时多点一点,以回家放菜为名 (3)理由要文雅 (4)开酒店的理由 第一时间触碰测试 (1)第一次约会要第一 ...

  5. PLSQL_性能优化系列14_Oracle High Water Level高水位分析

    2014-10-04 Created By BaoXinjian 一.摘要 PLSQL_性能优化系列14_Oracle High Water Level高水位分析 高水位线好比水库中储水的水位线,用于 ...

  6. OAF_EO系列1 - Definition定义(概念)

    2014-06-14 Created By BaoXinjian

  7. 错误记录 git pull

    在安装open-falcon的nodata组件,更新库的时候,git pull 报错: You are not currently on a branch, so I cannot use any'b ...

  8. java finally中含return语句

    <java核心技术卷一>中提到过:当finally子句包含return 语句时(当然在设计原则上是不允许在finally块中抛出异常或者 执行return语句的,我不明白为何java的设计 ...

  9. ylbtech-dbs:ylbtech-1,FAM(家庭资产管理系统)

    ylbtech-dbs:ylbtech-1,FAM(家庭资产管理系统) -- =============================================-- Family Assets ...

  10. Web Uploader文件上传&&使用webupload有感(黄色部分)

    引入资源 使用Web Uploader文件上传需要引入三种资源:JS, CSS, SWF. <!--引入CSS--> <link rel="stylesheet" ...