1 uplodify 批量上传图片(实质是一张一张upload)
上传控件:
<input type="file" name="file_upload" id="file_upload" οnclick="uploadFile();"/>
初始化uplodify:
//初始化上传控件 function initUploadifyNew() { $("#file_upload").uploadify({ 'buttonImage': '/Content/images/btn_upload.png', 'buttonText': '批量上传素材', 'height': 30, 'width': 105, 'swf': '/Scripts/uploadify-v3.2/uploadify.swf', 'uploader': '/ImageUpLoad/Import', 'cancelImg': '/Scripts/uploadify-v3.2/uploadify-cancel.png', 'fileTypeDesc': 'Image Files or Flash Files', 'fileTypeExts': '*.gif; *.jpg; *.png;*.bmp;', 'multi': true, 'auto': true, 'sizeLimit': 1024 * 1024 * 1, //1M 'onDialogOpen': function () { querryInfo.errMsg.html(""); }, 'onUploadSuccess': uploadSuccessNew, 'onUploadError': uploadErrorNew }); } function uploadErrorNew(file, errorCode, errorMsg, errorString) { alert("上传失败!"); } //上传成功--处理函数 function uploadSuccessNew(file, data, response) { var rel = JSON.parse(data); if (rel.msg.Status == "1") { var info = new Array(); info.push({ "fileName": rel.Title, "imgUrl": rel.Path }); var uploadInfo = eval("(" + JSON.stringify(info) + ")"); var tempOpt = new Array(); tempOpt.push("upload_info_temp"); var html = InitButtonHtml(tempOpt, uploadInfo); $("#upLoadInfo").show().append(html); initAutocompletes(); } else { querryInfo.errMsg.show(); querryInfo.errMsg.append(rel.msg.Message); } }
图片校验、保存至FTP服务器
[HttpPost] public ActionResult Import(HttpPostedFileBase FileData, string folder) { object obj = _imageUploadManagementBusiness.ImgUpload(FileData); return Json(obj, JsonRequestBehavior.AllowGet); }
public object ImgUpload(HttpPostedFileBase FileData) { object imgInfo = new object(); string physicsPath = string.Empty; string resultPath = string.Empty; MessageInfo Msg = new MessageInfo {Status = MessageStatus.Success}; try { if (FileData != null && FileData.ContentLength > 0) { var file = FileData; int filesize = Convert.ToInt32(ConfigHelper.AdverOpeningUploadFileSize); string[] fileExts = ConfigHelper.AdverOpeningUploadImgExts.Split(','); string upfileExtension = Path.GetExtension(file.FileName); string relativePath = @"~/Content/tempimages/" + Guid.NewGuid() + upfileExtension; physicsPath = HttpContext.Current.Server.MapPath(relativePath); if (file.ContentLength > filesize) { throw new Exception(string.Format("{0} 的图片大于1M,无法上传!", FileData.FileName)); } file.SaveAs(physicsPath); if (File.Exists(physicsPath)) { resultPath = SaveToFTP(relativePath); } } } catch (Exception e) { Msg.Message = e.Message; Msg.Status = MessageStatus.Error; } return new { Title = FileData.FileName, Path = resultPath, msg = Msg }; } private string SaveToFTP(string localPath) { string savefileName = string.Format("{0}_{1}{2}", Path.GetRandomFileName(), DateTime.Now.ToString("yyyymmdd"), Path.GetExtension(localPath)); using (FileStream fs = new FileStream(HttpContext.Current.Server.MapPath(localPath), FileMode.Open)) { string ftpAddr = AppSettings.FtpAddress; string ftpUser = AppSettings.FtpUser; string ftpPwd = AppSettings.FtpPwd; string FtpDir = AppSettings.FtpDir; FTPHelper.UploadCreativeToFTP(ftpAddr, fs, ftpUser, ftpPwd, FtpDir, savefileName); } return Path.Combine(AppSettings.PlayUrl, savefileName); }