纯代码不解释。

在CusotmerControllers中添加上传方法

/// <summary>
/// ajax上传用户头像
/// </summary>
/// <param name="uploadedFile"></param>
/// <returns></returns>
[HttpPost]
public string AjaxUploadAvatar(HttpPostedFileBase uploadedFile)
{
string message = string.Empty;
var customer = _workContext.CurrentCustomer;
try
{
var customerAvatar = _pictureService.GetPictureById(customer.GetAttribute<int>(SystemCustomerAttributeNames.AvatarPictureId));
if ((uploadedFile != null) && (!String.IsNullOrEmpty(uploadedFile.FileName)))
{
int avatarMaxSize = _customerSettings.AvatarMaximumSizeBytes;
if (uploadedFile.ContentLength > avatarMaxSize)
{
message = string.Format(_localizationService.GetResource("Account.Avatar.MaximumUploadedFileSize"), avatarMaxSize); return message;
} byte[] customerPictureBinary = uploadedFile.GetPictureBits();
if (customerAvatar != null)
customerAvatar = _pictureService.UpdatePicture(customerAvatar.Id, customerPictureBinary, uploadedFile.ContentType, null, true);
else
customerAvatar = _pictureService.InsertPicture(customerPictureBinary, uploadedFile.ContentType, null, true);
} int customerAvatarId = 0;
if (customerAvatar != null)
customerAvatarId = customerAvatar.Id; _genericAttributeService.SaveAttribute(customer, SystemCustomerAttributeNames.AvatarPictureId, customerAvatarId);
message = _pictureService.GetPictureUrl(
customer.GetAttribute<int>(SystemCustomerAttributeNames.AvatarPictureId),
_mediaSettings.AvatarPictureSize,
false);
return "1|" + message; }
catch (Exception exc)
{
message = exc.Message;
return message;
}
}

View代码:Avatar.cshtml

<script src="http://malsup.github.com/jquery.form.js" type="text/javascript"></script>
@using (Html.BeginForm("AjaxUploadAvatar", "Customer", FormMethod.Post, new { enctype = "multipart/form-data", id = "formUploadImg" }))
{
<div class="theme-popover-mask"></div>
<div class="theme-popover">
<div class="message-error">
@Html.ValidationSummary(true)
</div>
<div class="theme-poptit">
<a href="javascript:;" title="关闭" class="close">×</a>
<h3>修改您的头像</h3>
</div>
<div class="theme-popbod dform">
<div class="upload_left">
@if (!String.IsNullOrEmpty(Model.AvatarUrl))
{
<img src="@(Model.AvatarUrl)" alt="avatar" />
}<p>当前头像</p>
</div>
<div class="upload_right">
<h3>请选择您电脑上的图片:</h3>
<input name="uploadedFile" id="uploadedFile" type="file" />
<input type="submit" id="btnUploadImg" name="upload-avatar" class="button-1 upload-avatar-button" value="@T("Common.Upload")" />
@if (!String.IsNullOrEmpty(Model.AvatarUrl))
{
<input type="submit" name="remove-avatar" class="button-2 remove-avatar-button" value="@T("Account.Avatar.RemoveAvatar")" />
}
<div id="progress" style="display: none">
<div id="bar">图片正在上传请稍等.....</div>
</div>
<br />
<div id="message"></div>
<input type="hidden" value="0" id="hidIsUpLoadimg" />
<p>@T("Account.Avatar.UploadRules")</p>
</div>
</div>
</div>
}
<script type="text/javascript">
$(document).ready(function ($) {
$('.user_infor img').click(function () {
popupCon();
});
$('.theme-poptit .close').click(function () {
popupBtn();
}); $(document).ready(function () {
var options = {
beforeSend: function () {
$("#progress").show();
},
success: function () {
$("#progress").hide();
},
complete: function (response) {
if (response.responseText.split('|')[0] == "1") {
$("#hidIsUpLoadimg").val(response.responseText);
$("#message").html("<font color='green'>图片上传成功,请刷新当前页面.</font>");
}
},
error: function () {
$("#message").html("<font color='red'>上传图片出错,请重新上传!</font>");
}
};
$("#formUploadImg").ajaxForm(options);
});
});
</script>

其它的不多说了。在用NopCommerce的欢迎加群讨论。

【NopCommerce 3.1】asp.net mvc 利用jQuery from.js上传用户头像的更多相关文章

  1. ASP.NET MVC使用jQuery无刷新上传

    昨晚网友有下载了一个jQuery无刷新上传的小功能,他尝试搬至ASP.NET MVC应用程序中去,在上传死活无效果.Insus.NET使用Teamviewer远程桌面,操作一下,果真是有问题.网友是说 ...

  2. 在ASP.NET MVC下实现单个图片上传, 客户端服务端双重限制图片大小和格式, 服务端裁剪图片

    在"MVC文件图片ajax上传轻量级解决方案,使用客户端JSAjaxFileUploader插件01-单文件上传"一文中,使用JSAjaxFileUploader这款插件实现了单文 ...

  3. 基于ASP.NET MVC 利用(Aspose+Pdfobject.js) 实现在线预览Word、Excel、PPT、PDF文件

    #region VS2010版本以及以上版本源码下载地址:http://download.csdn.net/download/u012949335/10231812 VS2012版本以及以上版本源码下 ...

  4. Asp.Net Mvc 使用WebUploader 多图片上传

    来博客园有一个月了,哈哈.在这里学到了很多东西.今天也来试着分享一下学到的东西.希望能和大家做朋友共同进步. 最近由于项目需要上传多张图片,对于我这只菜鸟来说,以前上传图片都是直接拖得控件啊,而且还是 ...

  5. ASP.NET MVC实现Excel文件的上传下载

    在应用系统开发当中,文件的上传和下载是非常普遍的需求.在基于.NET的C/S架构的项目开发当中,有多种方案可以实现文件的上传和下载(httpwebrequest.webclient等),而且多采用异步 ...

  6. ASP.NET MVC 5 with EF 6 上传文件

        参考   ASP.NET MVC 5 with EF 6 - Working With Files Rename, Resize, Upload Image (ASP.NET MVC) ASP ...

  7. C#工具:ASP.NET MVC单例模式(懒汉)实现文件上传

    1.SingletonConfigRead帮助类 using System; using System.Collections.Generic; using System.IO; using Syst ...

  8. asp.net mvc 使用uploadfiles 实现异步上传数据

    lesg.cn 文章发布在:  http://www.lesg.cn/netdaima/net/2017-990.html 在实际开发过程中, 为了提高用户的体验,在上传文件的时候通常会使用异步上传文 ...

  9. Asp.net MVC 利用(aspose+pdfobject.js) 实现在线预览word、excel、ppt、pdf文件

    在线预览word.excel.ppt利用aspose动态生成html 主要代码 private bool OfficeDocumentToHtml(string sourceDoc, string s ...

随机推荐

  1. 前端WEB编辑器-------webstrom

    欲先善其事,必先利其器,如题.看到网上一篇介绍webstrom的文章,觉得功能确实强大,也知道为什么阿里巴巴的前端传到github上的文件为啥都有一个 .idea 文件,(传说淘宝内部推荐写js用we ...

  2. Javascript Number

    Number 对象 Number对象是原始值的包装对象 创建Number对象的语法: var myNum = new Number(value): var myNum = Number(value): ...

  3. Uploadify 3.2 参数属性、事件、方法函数详解以及配置

    一.属性 属性名称 默认值 说明 auto true 设置为true当选择文件后就直接上传了,为false需要点击上传按钮才上传 . buttonClass ” 按钮样式 buttonCursor ‘ ...

  4. World Wind Java开发之六——解析shape文件(转)

    http://blog.csdn.net/giser_whu/article/details/41647117 最近一直忙于导师项目的事情了,几天没更新了,昨天和今天研究了下WWJ解析shp文件的源代 ...

  5. 关于SIGSEGV错误及处理方法

    http://blog.csdn.net/brace/article/details/1102422 今天编程遇到了SIGSEGV错误,比较困惑,所以找了些资料,总结一下: (1)官方说法是: SIG ...

  6. 【51nod1677】treecnt(树上数学题)

    点此看题面 大致题意: 给你一个节点从1~n编号的树,让你从中选择k个节点并通过选择的边联通,且要使选择的边数最少,让你计算对于所有选择k个节点的情况最小选择边数的总和. 题解 这道题乍一看很麻烦:最 ...

  7. 查看数据库表存储引擎MyISAM/InnoDB

    Mysql: show table status *MyISAM不支持PDO的事务

  8. python_77_json与pickle序列化3

    #此方法:dump多次,而不可以load多次,只能load一次,否则会出错 只有序列化,无反序列化 import json info={ 'name':'Xue Jingjie', 'age':22, ...

  9. c# 科学计数法值转换成正常值,返回字符串

    /// <summary> /// 科学计数法值转换成正常值 /// </summary> /// <param name="value">&l ...

  10. iOS 闭包传值 和 代理传值

    let vc = ViewController() let navc = UINavigationController(rootViewController: vc) window = UIWindo ...