前端js
function uploadImg1() {
var f = document.getElementById('uploadFile1').files[0]
var src = window.URL.createObjectURL(f);
document.getElementById('uploadPreview1').src = src;
learun.loading(true, '正在保存...');
$.ajaxFileUpload({
url: top.$.rootUrl + "/LR_PortalSite/HomeConfig/UploadFile?type=4",
secureuri: false,
fileElementId: 'uploadFile1',
dataType: 'json',
success: function (data) {
learun.loading(false);
}
});
};
后台
/// <summary>
/// 保存图片和存储数据
/// </summary>
/// <param name="type">类型</param>
/// <returns></returns>
[HttpPost]
public ActionResult UploadFile(string type)
{
HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
//没有文件上传,直接返回
if (files[0].ContentLength == 0 || string.IsNullOrEmpty(files[0].FileName))
{
return HttpNotFound();
}
else
{
string FileEextension = Path.GetExtension(files[0].FileName);
byte[] bytes = new byte[files[0].InputStream.Length];
files[0].InputStream.Read(bytes, 0, bytes.Length);
string strBase64 = Convert.ToBase64String(bytes);
homeConfigIBLL.SaveImg(strBase64, files[0].FileName, FileEextension, type);
}
return Success("保存成功。");
}
获取后台
public void GetImg(string keyValue)
{
ArticleEntity entity = GetEntity(keyValue);
string img = "";
if (entity != null && !string.IsNullOrEmpty(entity.F_Img))
{
ImgEntity imgEntity = imgIBLL.GetEntity(entity.F_Img);
if (imgEntity != null && !string.IsNullOrEmpty(imgEntity.F_Content))
{
FileDownHelper.DownLoadBase64(imgEntity.F_Content, imgEntity.F_Name);
return;
}
}
else
{
img = "/Content/images/plhome/banner_df.jpg";
}
if (string.IsNullOrEmpty(img))
{
img = "/Content/images/plhome/banner_df.jpg";
}
FileDownHelper.DownLoad(img);
}