关于.net上传图片不能预览的问题
来源:互联网 发布:软件数据储存在哪里 编辑:程序博客网 时间:2024/05/18 03:21
项目中的WebConfig页面必须要引用下面一段代码
<appSettings >
<add key="uploadify_root" value="/upload/uploadify/"/></appSettings>
并且项目中要建立对应的文件夹来保存图片文件
下面是ashx文件
#region 上传图片
/// <summary>
/// 上传图片
/// </summary>
/// <param name="context"></param>
private void UploadImage(HttpContext context)
{
string strRoot = System.Configuration.ConfigurationManager.AppSettings["uploadify_root"];
StringBuilder sbMsg = new StringBuilder();
String fileTypes = "gif,jpg,jpeg,png";
int maxSize = 1024 * 1024 * 2;
HttpPostedFile imgFile = context.Request.Files["Filedata"];
if (imgFile == null)
{
sbMsg.Append("{\"code\":\"201\",\"tips\":\"请上传图片!\"}");
context.Response.Write(sbMsg.ToString());
context.Response.End();
}
String dirPath = context.Server.MapPath(strRoot);
if (!Directory.Exists(dirPath))
{
try
{
Directory.CreateDirectory(strRoot);
}
catch
{
sbMsg.Append("{\"code\":\"201\",\"tips\":\"保存目录创建失败,请检查文件夹权限!\"}");
context.Response.Write(sbMsg.ToString());
context.Response.End();
}
}
String fileName = imgFile.FileName;
String fileExt = Path.GetExtension(fileName).ToLower();
ArrayList fileTypeList = ArrayList.Adapter(fileTypes.Split(','));
if (imgFile.InputStream.Length > maxSize)
{
sbMsg.Append("{\"code\":\"201\",\"tips\":\"超出最大上传限制(2M)!\"}");
context.Response.Write(sbMsg.ToString());
context.Response.End();
}
if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(fileTypes.Split(','), fileExt.Substring(1).ToLower()) == -1)
{
sbMsg.Append("{\"code\":\"201\",\"tips\":\"请上传指定格式的图片文件!\"}");
context.Response.Write(sbMsg.ToString());
context.Response.End();
}
try
{
String ymd = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
dirPath += ymd + "/";
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
string newDate = DateTime.Now.ToString("yyyyMMddHHmmssffff", DateTimeFormatInfo.InvariantInfo);
string strfn = StringUtility.FilterSpecial(imgFile.FileName);
String newFileName = strfn.Remove(strfn.Length - fileExt.Length, fileExt.Length) + "_" + newDate + fileExt;
//检测文件是否重名
if (File.Exists(context.Server.MapPath(strRoot + ymd + "/" + newFileName)))
{
sbMsg.Append("{\"code\":\"201\",\"tips\":\"已存在重名文件!\"}");
context.Response.Write(sbMsg.ToString());
context.Response.End();
}
String filePath = dirPath + newFileName;
imgFile.SaveAs(filePath);
String fileUrl = strRoot + ymd + "/" + newFileName;
sbMsg.Append("{\"code\":\"200\",\"tips\":\"上传成功!\",\"url\":\"" + fileUrl.Replace("\\", "\\\\") + "\"}");
context.Response.Write(sbMsg);
}
catch
{
context.Response.Write("{\"code\":\"201\",\"tips\":\"上传失败,请刷新后重试!\"}");
}
}
#endregion
下面是ajax
//upload files
$("#uploadify").uploadify({
'uploader': 'ajax/fileUpload.ashx?action=upload_image',//指定一般处理程序 执行上传后的文件处理
'fileSizeLimit': '2MB',
'multi': false,
'fileTypeDesc': '图片文件',
'fileTypeExts': '*.gif; *.jpg; *.png',
'onUploadSuccess': function (file, data, response) {
var msg = $.parseJSON(data);
if (msg.code == "200") {
$('#img_show').attr('src', msg.url);
$('#videocover').val(msg.url);
} else {
Public.showMessage(false, msg.tips);
}
}
});
注意ajax、获取的值 一定要在下面的id中获取
<td rowspan="6">图片预览
<div style="margin: 5px;" id="file_upload">
<input type="hidden" id="videocover" name="videocover" value="<%=strPicture %>" />
<img id="img_show" src="<%=strPicture %>" style="width: 185px; height: 150px; border: solid 1px #eeeeee;" />
</div>
</td>
- 关于.net上传图片不能预览的问题
- .net 上传图片预览
- net 预览上传图片
- 上传图片预览问题
- 上传图片预览问题
- 对于支持上传文件的手机浏览器出现的图片不能预览问题的解决办法
- .NET使用ueditor时不能上传图片的问题
- ckeditor的图片上传、预览问题
- 图片上传之前的预览问题
- 关于界面不能预览的问题
- 关于php上传多张图片时,选择图片后就可以预览的问题
- asp.net带预览的图片上传客户端脚本
- 关于H5里的API,上传图片预览功能
- 解决IE7下不能预览图片的问题
- 上传图片前的预览
- 预览待上传的图片
- 预览上传的本地图片
- 上传图片前的预览
- 电子现金 圈存、补登
- 转换字符串格式为原来字符串里字符+该字符连续出现的个数
- JS与iOS之间的通信 iOS调用JS代码
- iOS: 在代码中使用Autolayout (1) - 按比例缩放和优先级
- json4s+rediscala实现kafka到spark streaming到redis
- 关于.net上传图片不能预览的问题
- nginx 重写 rewrite 基础及实例
- linux下安装及启动mysql
- 元素唯一性问题(C实现)
- HTML标识符
- 解决 证书报 UUID 不匹配问题 Your build settings specify a provisioning profile with the UUID
- SharedPreferences的概念和使用
- Java WEB实现URL重写的优缺点及如何实现
- ARM7 s3c44box中断全局变量问题