纯代码不解释。

在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. cron 任务执行表达式

    1.来源 开始我还不知道cron到底来源于哪里,不求甚解的我也没做过多了解,现在突然用到所以写一下. cron计划任务  其实只是linux 一个执行计划的一个工具或者执行程序. 在Linux系统中, ...

  2. tf warning等级

    from:http://blog.csdn.net/tsinghuahui/article/details/72938764 tf讨厌的warning 2017-08-03 10:02:52.0990 ...

  3. touch事件总结

    $("body").on("touchstart", function(e) { e.preventDefault(); startX = e.original ...

  4. javascript HTML静态页面传值的四种方法

    一:JavaScript静态页面值传递之URL篇能过URL进行传值.把要传递的信息接在URL上.Post.htm 代码如下: <input type="text" name= ...

  5. window7防火墙无法更改某些设置,错误代码0×80070422

    原因:这是由于管理工具的服务中的windows防火墙被禁用了. 解决方案:在window7中点击控制面板,然后点击管理工具,在点服务,然后找到windows firewall 然后将其改为自动就就可以 ...

  6. linux 命令——37 date (转)

    在linux环境中,不管是编程还是其他维护,时间是必不可少的,也经常会用到时间的运算,熟练运用date命令来表示自己想要表示的时间,肯定可以给自己的工作带来诸多方便. 1.命令格式: date [参数 ...

  7. Aizu 2456 Usoperanto (贪心)

    贪心,对于一个修饰关系可以连一条有向边,在合并的时候,子节点的序列一定是连续安排的,因为如果有交叉,交换以后一定更优. 然后一个序列一个序列的考虑,长度短的应该在前面,否则同样交换以后更优.因此排序以 ...

  8. 【洛谷4009】汽车加油行驶问题(SPFA乱搞)

    点此看题面 大致题意:给定一个\(N*N\)的方形网格,其中1表示这个格子有油库,0表示这个格子没油库,且汽车加满油可以行驶\(k\)条网格边.如果遇到油库必须加满油并花费\(A\)元,如果\(X\) ...

  9. Java操作Redis工具类

    依赖 jar 包 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis< ...

  10. 如何更改VirtualBox虚拟电脑内存大小