异步上传头像
前台:
<form id="FileForm">
<span style="width: 100px;">
<img id="oldimghead" src="~/Content/images/HeadPortraitMan.jpg" style="width: 100px; height: 100px;" />
<img id="imghead" src="" style="display: none; width: 100px; height: 100px;" />
</span>
<span id="uploadImg" style="margin-left: 10px; width: 100px; height: 100px;">
<input type="file" name="file" onchange="previewImage()" id="file" value="上传资料" /><img src="~/Content/images/ModifyHead.jpg" />
</span>
</form>
js:
function previewImage() {
$("#oldimghead").hide();
$("#imghead").show();
$("#FileForm").ajaxSubmit({
success: function (url) {
ImageUrl = "/FileServer/UserPhoto" + url;
if (url != "" && url != null) {
$("#imghead").attr("src", ImageUrl);
}
else
$("#imghead").attr("src", "~/Content/images/HeadPortraitMan.jpg");
},
error: function (error) { alert(error); },
url: '/Member/UploadPhoto', /*设置post提交到的页面*/
type: "post", /*设置表单以post方法提交*/
dataType: "json" /*设置返回值类型为文本*/
});
}
后台:
//上传头像
public ActionResult UploadPhoto()
{
// var LoginModel = Session["UserLogin"] != null ? Session["UserLogin"] as LoginModel : null;
// int CusACCPInfoID = LoginModel.CusACCPInfo_ID;//客户联系人ID
int CusACCPInfoID = 20;
HttpPostedFileBase hp = Request.Files["file"];
string uploadPath = Server.MapPath("/FileServer/UserPhoto");
var msg = UploadHelper.UploadImage(CusACCPInfoID, 1, hp, uploadPath);
return Json(msg, JsonRequestBehavior.AllowGet);
}
服务器存储:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace WMJQ.Common
{
public static class UploadHelper
{
/// <summary>
/// 图片上传
/// </summary>
/// <param name="CustomID">客户个性ID</param>
/// <param name="CConactID">客户联系人个性ID</param>
/// <param name="hp">图片资源</param>
/// <param name="uploadPath">Base路径</param>
/// <returns></returns>
public static string UploadImage(int CustomID, int CConactID, HttpPostedFileBase hp, string uploadPath)
{
//定义错误消息
string msg = "";
if (hp == null)
{
msg = "请选择文件.";
}
//获取文件名
string fileName = DateTime.Now.ToString("yyyyMMddHHmmssms") + System.IO.Path.GetExtension(hp.FileName); ;
//获取文件大小
long contentLength = hp.ContentLength;
//文件不能大于1M
if (contentLength > 1024 * 1024 * 2)
{
msg = "文件大小超过限制要求.";
}
if (!checkFileExtension(hp.FileName))
{
msg = "文件为不可上传的类型.";
}
//保存文件的物理路径
string saveFilePath = "/" + CustomID + "/" + CConactID ;
string saveFile = uploadPath + saveFilePath + "/" + fileName;
Directory.CreateDirectory(uploadPath + saveFilePath);
try
{
//保存文件
hp.SaveAs(saveFile);
msg = saveFilePath + "/" + fileName;
}
catch
{
msg = "上传失败.";
}
return msg;
}
/// <summary>
/// 检查文件后缀名是否符合要求
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
private static bool checkFileExtension(string fileName)
{
//获取文件后缀
string fileExtension = System.IO.Path.GetExtension(fileName);
if (fileExtension != null)
fileExtension = fileExtension.ToLower();
else
return false;
if (fileExtension != ".jpeg" && fileExtension != ".gif" && fileExtension != ".png" && fileExtension != ".jpg")
return false;
return true;
}
}
}