【NopCommerce 3.1】asp.net mvc 利用jQuery from.js上传用户头像
纯代码不解释。
在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上传用户头像的更多相关文章
- ASP.NET MVC使用jQuery无刷新上传
昨晚网友有下载了一个jQuery无刷新上传的小功能,他尝试搬至ASP.NET MVC应用程序中去,在上传死活无效果.Insus.NET使用Teamviewer远程桌面,操作一下,果真是有问题.网友是说 ...
- 在ASP.NET MVC下实现单个图片上传, 客户端服务端双重限制图片大小和格式, 服务端裁剪图片
在"MVC文件图片ajax上传轻量级解决方案,使用客户端JSAjaxFileUploader插件01-单文件上传"一文中,使用JSAjaxFileUploader这款插件实现了单文 ...
- 基于ASP.NET MVC 利用(Aspose+Pdfobject.js) 实现在线预览Word、Excel、PPT、PDF文件
#region VS2010版本以及以上版本源码下载地址:http://download.csdn.net/download/u012949335/10231812 VS2012版本以及以上版本源码下 ...
- Asp.Net Mvc 使用WebUploader 多图片上传
来博客园有一个月了,哈哈.在这里学到了很多东西.今天也来试着分享一下学到的东西.希望能和大家做朋友共同进步. 最近由于项目需要上传多张图片,对于我这只菜鸟来说,以前上传图片都是直接拖得控件啊,而且还是 ...
- ASP.NET MVC实现Excel文件的上传下载
在应用系统开发当中,文件的上传和下载是非常普遍的需求.在基于.NET的C/S架构的项目开发当中,有多种方案可以实现文件的上传和下载(httpwebrequest.webclient等),而且多采用异步 ...
- 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 ...
- C#工具:ASP.NET MVC单例模式(懒汉)实现文件上传
1.SingletonConfigRead帮助类 using System; using System.Collections.Generic; using System.IO; using Syst ...
- asp.net mvc 使用uploadfiles 实现异步上传数据
lesg.cn 文章发布在: http://www.lesg.cn/netdaima/net/2017-990.html 在实际开发过程中, 为了提高用户的体验,在上传文件的时候通常会使用异步上传文 ...
- Asp.net MVC 利用(aspose+pdfobject.js) 实现在线预览word、excel、ppt、pdf文件
在线预览word.excel.ppt利用aspose动态生成html 主要代码 private bool OfficeDocumentToHtml(string sourceDoc, string s ...
随机推荐
- 域名带与不带www的区别
首先我们来说下为什么会产生带www与不带www的两种域名. 不带www的域名为顶级域名或一级域名,如qiankoo.com.带www的为二级域名,如www.qiankoo.com. 在购买域名时,域名 ...
- 如何使用VS将项目生成一个安装包?
VS2010项目的部署与安装winform程序,我想进行安装.1.在解决方案中 ——点击右键——添加 2.然后选择 安装和部署 ——安装向导 可以更改名称 3.点击 下一步 4.然后选择上那3个 5. ...
- arcgis api for js 地图查询
arcgis api for js入门开发系列四地图查询(含源代码) 上一篇实现了demo的地图工具栏,本篇新增地图查询功能,包括属性查询和空间查询两大块,截图如下: 属性查询效果图: 空间查询效 ...
- yum安装软件并保留下载的软件
使用yum插件downloadonly下载安装软件需要的依赖包并保留到指定的文件 安装yum-downloadonly或 yum-plugin-downloadonly 软件包. yum instal ...
- mybatis-映射器的CRUD
设计步骤:model.mapper.dao.service.junit单元测试.log4j日志 项目和之前的一样在此只是创建了test和修改了mapper 1.修改映射 1.1修改接口 package ...
- 查询MySQL的存储引擎
- SQLSERVER 创建ODBC 报错的解决办法 SQLState:'01000'的解决方案
错误详情如下: SQLState:'01000' SQL Server 错误:14 [Microsoft][ODBC SQL Server Driver][DBNETLIB] ConnectionOp ...
- 【BZOJ2754】[SCOI2012] 喵星球上的点名(后缀数组+莫队)
点此看题面 大致题意: 每个人的名字由姓和名构成,如果某次点名点到的字符串是某人姓或名的一个子串,则这个人就被点到了.求每次点名被点到的人的个数及每个人被点到的总次数. 后缀数组+莫队 这道题做法很多 ...
- 【BZOJ1087】[SCOI2005] 互不侵犯King(状压DP)
点此看题面 大致题意: 在\(N×N\)的棋盘里面放\(K\)个国王,使他们互不攻击,共有多少种摆放方案(国王能攻击到它周围的8个格子). 状压\(DP\) 一看到这道题我就想到了经典的八皇后问题,但 ...
- 【转载】WEB架构师成长之路
本人也是coding很多年,虽然很失败,但也总算有点失败的心得,不过我在中国,大多数程序员都是像我一样,在一直走着弯路,如果想成为一个架构师,就必须走正确的路,否则离目标越来越远,正在辛苦工作的程序员 ...