JQYERY.ajaxFileUpload 中AJAX上传图片和声音
//前台JS
if (/^.+\.(gif|jpg|jpeg|png|bmp)$/i.test(upload.toLowerCase())) { //图片验证格式
//if (/^.+\.(mp3|wav|rm|ape|wma)$/i.test(uploadSound.toLowerCase())) { //声音验证格式
$.ajaxFileUpload({
url: "../ajax/crm/MemoHandler.ashx",
secureuri: false,
fileElementId: 'upload',
dataType: 'text',
success: function (data) {
if (data == "1") {
alert("上传的图片不得大于500K");
return false;
} else if (data == "2") {
alert("服务器繁忙,请稍后再试!");
return false;
} else if (data == "3") {//add by wbliu at 2012-09-20 on 上传伪图片显示成声音播放Bug修复
alert("上传的必须为图片!");
return false;
} else {
parent.doUploadPic(tTime, ctype, data.toString());
}
}
});
}
else {
alert("文件类型只支持gif、jpg、jpeg、bmp和png格式!");
document.getElementById("upload").outerHTML = document.getElementById("upload").outerHTML; //清空文件选择框中的内容
return false;
}
//AJAX中判断
HttpFileCollection files = HttpContext.Current.Request.Files;
HRSO.BLL.PicBLL picBLL = new PicBLL();
if (files.Count > 0){
HttpPostedFile file = files[0];//这样取上传文件对象
try{
int fileCntentLength = files[0].ContentLength;
if (fileCntentLength > 0){
if (fileCntentLength > 500 * 1024){
resultStr = "1"; //判断是否大于500K
}else{
//begin add by wbliu at 2012-09-20 on 上传伪图片显示成声音播放Bug修复
string fileContentType = file.ContentType;
if (fileContentType == "image/gif" || fileContentType == "image/jpg" || fileContentType == "image/pjpeg" || fileContentType == "image/jpeg" || fileContentType == "image/x-png" || fileContentType == "image/bmp")
{
resultStr = picBLL.savePicToDatabase(fileCntentLength, files[0].InputStream, "T_Memo_ContentPath"); //保存到图片数据库返回guid(字符串类型)做为图片url
}
else
{
resultStr = "3";
}
//end
}
}
}catch (Exception){
resultStr = "2"; //异常
}
context.Response.Write(resultStr);
}
//父页面执行操作
/*执行图片上传*/
function doUploadPic(tTime, ctype, pickey) {
hidCreateNoteMain();
$.ajax({
type: "post",
url: "../ajax/crm/MemoHandler.ashx",
data: "action=uppic&ctime=" + encodeURI(tTime) + "&pickey=" + encodeURI(pickey) + "&ctype=" + ctype + "&rand=" + Math.random(), //tTime:提醒时间;pickey:图片;ctype:
dataType: "text",
success: function (data) {
if (data > 0) {
alert("添加成功!");
BindCalendar();
} else {
alert("系统繁忙,请稍后再试!");
}
}
});
}
//父页面AJAX中执行
if (action == "uppic") {
DateTime ctime = Convert.ToDateTime(HttpUtility.UrlDecode(context.Request["ctime"].ToString())); //提醒时间
string pickey = HttpUtility.UrlDecode(context.Request["pickey"].ToString()); //备注图片标识
string ctype = Convert.ToString(context.Request["ctype"]); //联系方式(1:电话;2:短信;3:email)
HRSO.BLL.CrmCustomGroupBLL crmCustomGroupBLL = new CrmCustomGroupBLL();
HRSO.Model.CrmMemo crmMemo = new CrmMemo();
crmMemo.CompanyKey = hrcompanykey;
crmMemo.UserKey = hrukey;
crmMemo.RemindTime = ctime;
crmMemo.RemindContent = "备注为图片!";
crmMemo.ReminderByEmail = ctype == "0" ? "N" : "Y";
crmMemo.ContentPath = pickey;
resultStr = crmCustomGroupBLL.AddMemo(crmMemo).ToString();
context.Response.Write(resultStr);
}
//图片和声音转化成流文件保存到数据库
#region 把图片存到图片数据库表中
/// <summary>
/// 把图片存到图片数据库表中
/// </summary>
/// <param name="fileContentLength">上传图片大小(如:FileUpload1.PostedFile.ContentLength;)</param>
/// <param name="inputStream">数据流对象(如:hp.InputStream;)</param>
/// <param name="picTable">上传的表名</param>
/// <returns>string</returns>
public string savePicToDatabase(int fileContentLength, Stream inputStream, string pictureTable)
{
Byte[] FileData = new Byte[fileContentLength];
//创建访问客户端上传文件的对象
//HttpPostedFile hp = FileUpload1.PostedFile;
//创建数据流对象
//Stream sr = hp.InputStream;
Stream sr = inputStream;
//将图片数据放到FileData数组对象实例中
sr.Read(FileData, 0, fileContentLength);
//调用存储图片二进制数据(数据库)方法函数
PicBLL picbll = new PicBLL();
Guid picid = picbll.AddPic(pictureTable, FileData);
return picid.ToString();
}
浙公网安备 33010602011771号