纯代码不解释。

在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. SQL Server(第二章) 字符串函数、日期时间函数、转换函数

    --1.CONCAT 函数:字符串连接(支持sql server2012 SQL规则 如果与NULL连接返回NILL) SELECT empid,CONCAT(firstname,lastname) ...

  2. Visual Studio 更改护眼颜色

  3. npm常用指令小记

    查看本地指定包在npm远程服务器的版本信息 方式一: npm view <packageName> versions 方式二: npm info <packageName> 查 ...

  4. linux 命令——11 nl (转)

    nl命令在linux系统中用来计算文件中行号.nl 可以将输出的文件内容自动的加上行号!其默认的结果与 cat -n 有点不太一样, nl 可以将行号做比较多的显示设计,包括位数与是否自动补齐 0 等 ...

  5. linux 命令——6 rmdir(转)

    今天学习一下linux中命令: rmdir命令.rmdir是常用的命令,该命令的功能是删除空目录,一个目录被删除之前必须是空的.(注意,rm - r dir命令可代替rmdir,但是有很大危险性.)删 ...

  6. jquery实现简单的验证码倒计时的效果

    HTML: <div class="container"> <form> <div class="form-group"> ...

  7. 爬虫爬取代理IP池及代理IP的验证

    最近项目内容需要引入代理IP去爬取内容. 为了项目持续运行,需要不断构造.维护.验证代理IP. 为了绕过服务端对IP 和 频率的限制,为了阻止服务端获取真正的主机IP. 一.服务器如何获取客户端IP ...

  8. [论文理解]Focal Loss for Dense Object Detection(Retina Net)

    Focal Loss for Dense Object Detection Intro 这又是一篇与何凯明大神有关的作品,文章主要解决了one-stage网络识别率普遍低于two-stage网络的问题 ...

  9. CentOS系统下安装Redis

    1. 安装C语言环境 yum install gcc-c++ 2.下载Redis安装包 http://download.redis.io/releases/redis-3.2.9.tar.gz 3.解 ...

  10. CentOS 7 防火墙 出现Failed to start iptables.service: Unit iptables.service failed to load

    错误信息如下: [root]# service iptables start Redirecting to /bin/systemctl start iptables.service Failed t ...