//DONT USE THIS IF YOU NEED TO ALLOW LARGE FILES UPLOADS //Credit to i-e-b and his ASP.Net uploader for the bulk of the upload helper methods - https://github.com/i-e-b/jQueryFileUpload.Net private Exception UploadWholeFile(HttpRequestBase request, List<ImgResultModel> result, ImgTarget e, bool userOriginalFileName) { Exception exn = null; for (int i = 0; i < request.Files.Count; i++) { string ext; string f2 = save(request.Files[i], e, out ext, userOriginalFileName); List<FileModel> list = new List<FileModel>(); if (e != ImgTarget.File) { string targetPath = StorageRoot + "\\" + getFloderName(e); string oPath = getFloderName(e); switch (e) { case ImgTarget.ReadingBookPic: list.Add(new FileModel() { FilePath = targetPath, OppositePath = oPath + "\\", FileName = f2, Content = ThumbnailOperation.MakethumbanilBookPic(Path.Combine(targetPath, f2)) }); break; case ImgTarget.MemberPhoto: list.Add(new FileModel() { FilePath = targetPath, OppositePath = oPath + "\\", FileName = f2, Content = ThumbnailOperation.MakeThumbnailMemberPhoto(Path.Combine(targetPath, f2)) }); break; case ImgTarget.AcivesPic: list.Add(new FileModel() { FilePath = targetPath, OppositePath = oPath + "\\", FileName = f2, Content = ThumbnailOperation.MakethumbanilActivesPic(Path.Combine(targetPath, f2)) }); break; case ImgTarget.ActivesAttachment: list.Add(new FileModel() { FilePath = targetPath, OppositePath = oPath + "\\", FileName = f2, Content = null });//原图 list.Add(new FileModel() { FilePath = targetPath + "\\S\\", OppositePath = oPath + "\\S\\", FileName = f2, Content = ThumbnailOperation.MakeThumbnail(Path.Combine(targetPath, f2), 106, 106, OperationModeEnum.AutoRefMinSize) }); list.Add(new FileModel() { FilePath = targetPath + "\\M\\", OppositePath = oPath + "\\M\\", FileName = f2, Content = ThumbnailOperation.MakeThumbnailLow(Path.Combine(targetPath, f2), 1024, 1024, OperationModeEnum.Ori) }); break; case ImgTarget.ReadingRecommendPic: list.Add(new FileModel() { FilePath = targetPath, OppositePath = oPath + "\\", FileName = f2, Content = ThumbnailOperation.MakeThumbnailLow(Path.Combine(targetPath, f2), 652, 205, OperationModeEnum.HW) }); break; case ImgTarget.CoursePic: list.Add(new FileModel() { FilePath = targetPath, OppositePath = oPath + "\\", FileName = f2, Content = ThumbnailOperation.MakeThumbanilCoursePic(Path.Combine(targetPath, f2)) }); break; case ImgTarget.CourseRollPic: list.Add(new FileModel() { FilePath = targetPath, OppositePath = oPath + "\\", FileName = f2, Content = ThumbnailOperation.MakeThumbnailLow(Path.Combine(targetPath, f2), 652, 205, OperationModeEnum.HW) }); break; case ImgTarget.WareContentPic: list.Add(new FileModel() { FilePath = targetPath, OppositePath = oPath + "\\", FileName = f2, Content = null });//原图 list.Add(new FileModel() { FilePath = targetPath + "\\S\\", OppositePath = oPath + "\\S\\", FileName = f2, Content = ThumbnailOperation.MakeThumbnail(Path.Combine(targetPath, f2), 380, 200, OperationModeEnum.Auto) }); list.Add(new FileModel() { FilePath = targetPath + "\\M\\", OppositePath = oPath + "\\M\\", FileName = f2, Content = ThumbnailOperation.MakeThumbnailLow(Path.Combine(targetPath, f2), 980, 520, OperationModeEnum.Ori) }); break; case ImgTarget.WareContentFlash: list.Add(new FileModel() { FilePath = targetPath, OppositePath = oPath + "\\", FileName = f2, Content = null });//原图 break; case ImgTarget.WareAttachment: list.Add(new FileModel() { FilePath = targetPath, OppositePath = oPath + "\\", FileName = f2, Content = null });//原图 break; case ImgTarget.PortalEspeciallyPlan: list.Add(new FileModel() { FilePath = targetPath, OppositePath = oPath + "\\", FileName = f2, Content = null });//原图 //list.Add(new FileModel() { FilePath = targetPath + "\\S\\", OppositePath = oPath + "\\S\\", FileName = f2, Content = ThumbnailOperation.MakeThumbnail(Path.Combine(targetPath, f2), 46, 46, OperationModeEnum.HW) }); list.Add(new FileModel() { FilePath = targetPath + "\\M\\", OppositePath = oPath + "\\M\\", FileName = f2, Content = ThumbnailOperation.MakeThumbnailLow(Path.Combine(targetPath, f2), 1000, 400, OperationModeEnum.HW) }); break; case ImgTarget.PortalEspeciallyPlanPreview: //list.Add(new FileModel() { FilePath = targetPath, OppositePath = oPath + "\\", FileName = f2, Content = null });//原图 list.Add(new FileModel() { FilePath = targetPath + "\\S\\", OppositePath = oPath + "\\S\\", FileName = f2, Content = ThumbnailOperation.MakeThumbnail(Path.Combine(targetPath, f2), 46, 46, OperationModeEnum.HW) }); //list.Add(new FileModel() { FilePath = targetPath + "\\M\\", OppositePath = oPath + "\\M\\", FileName = f2, Content = ThumbnailOperation.MakeThumbnailLow(Path.Combine(targetPath, f2), 1000, 400, OperationModeEnum.HW) }); break; case ImgTarget.PortalEspeciallyPlanHP: list.Add(new FileModel() { FilePath = targetPath, OppositePath = oPath + "\\", FileName = f2, Content = null });//原图 //list.Add(new FileModel() { FilePath = targetPath + "\\S\\", OppositePath = oPath + "\\S\\", FileName = f2, Content = ThumbnailOperation.MakeThumbnail(Path.Combine(targetPath, f2), 46, 46, OperationModeEnum.HW) }); //list.Add(new FileModel() { FilePath = targetPath + "\\M\\", OppositePath = oPath + "\\M\\", FileName = f2, Content = ThumbnailOperation.MakeThumbnailLow(Path.Combine(targetPath, f2), 1000, 400, OperationModeEnum.HW) }); break; case ImgTarget.PortalSpecialRecommendCover: list.Add(new FileModel() { FilePath = targetPath, OppositePath = oPath + "\\", FileName = f2, Content = null });//原图 break; case ImgTarget.CircleCover: list.Add(new FileModel() { FilePath = targetPath, OppositePath = oPath + "\\", FileName = f2, Content = ThumbnailOperation.MakeThumbnail(Path.Combine(targetPath, f2), 177, 177, OperationModeEnum.HW) }); break; case ImgTarget.CircleFileAttachment: list.Add(new FileModel() { FilePath = targetPath, OppositePath = oPath + "\\", FileName = f2, Content = null }); break; case ImgTarget.CirclePhotoAttachment: list.Add(new FileModel() { FilePath = targetPath, OppositePath = oPath + "\\", FileName = f2, Content = null });//原图 list.Add(new FileModel() { FilePath = targetPath + "\\S\\", OppositePath = oPath + "\\S\\", FileName = f2, Content = ThumbnailOperation.MakeThumbnail(Path.Combine(targetPath, f2), 100, 100, OperationModeEnum.AutoRefMinSize) }); break; case ImgTarget.SharePic: list.Add(new FileModel() { FilePath = targetPath, OppositePath = oPath + "\\", FileName = f2, Content = null });//原图 list.Add(new FileModel() { FilePath = targetPath + "\\S\\", OppositePath = oPath + "\\S\\", FileName = f2, Content = ThumbnailOperation.MakeThumbnail(Path.Combine(targetPath, f2), 100, 100, OperationModeEnum.HW) }); break; case ImgTarget.LevelIco: list.Add(new FileModel() { FilePath = targetPath, OppositePath = oPath + "\\", FileName = f2, Content = ThumbnailOperation.MakeThumbnail(Path.Combine(targetPath, f2), 100, 100, OperationModeEnum.HW) }); break; case ImgTarget.TeamChargePersonPic: list.Add(new FileModel() { FilePath = targetPath, OppositePath = oPath + "\\", FileName = f2, Content = null });//原图 break; case ImgTarget.TeamHonorPic: list.Add(new FileModel() { FilePath = targetPath, OppositePath = oPath + "\\", FileName = f2, Content = null });//原图 break; case ImgTarget.TeamLogo: list.Add(new FileModel() { FilePath = targetPath + "\\", OppositePath = oPath + "\\", FileName = f2, Content = ThumbnailOperation.MakeThumbnail(Path.Combine(targetPath, f2), 200, 200, OperationModeEnum.AutoRefMinSize) }); break; case ImgTarget.RecommendTeamPic: list.Add(new FileModel() { FilePath = targetPath + "\\", OppositePath = oPath + "\\", FileName = f2, Content = ThumbnailOperation.MakeThumbnail(Path.Combine(targetPath, f2), 652, 300, OperationModeEnum.AutoRefMinSize) }); break; default: break; } foreach (FileModel m in list) { if (m.Content == null) { result.Add(new ImgResultModel() { FileName = m.FileName, Type = request.Files[i].ContentType, Domain = XXW.SiteUtils.SiteAddressHelper.Instance.GetUrl(XXW.Enum.SiteTypeEnum.ResourceSite), path = (m.OppositePath).Replace("\\", "/"), Size = request.Files[i].ContentLength }); } else { MemoryStream ms = new MemoryStream(m.Content); Bitmap bitMap = new Bitmap(ms); try { if (!Directory.Exists(m.FilePath)) Directory.CreateDirectory(m.FilePath); bitMap.Save(m.FilePath + "\\" + m.FileName); } catch (Exception ex) { int a = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); exn = ex; } finally { bitMap.Dispose(); ms.Close(); } result.Add(new ImgResultModel() { FileName = m.FileName, Type = request.Files[i].ContentType, Domain = XXW.SiteUtils.SiteAddressHelper.Instance.GetUrl(XXW.Enum.SiteTypeEnum.ResourceSite), path = (m.OppositePath).Replace("\\", "/"), Size = request.Files[i].ContentLength }); } } } } return exn; }
//DONT USE THIS IF YOU NEED TO ALLOW LARGE FILES UPLOADS //Credit to i-e-b and his ASP.Net uploader for the bulk of the upload helper methods - https://github.com/i-e-b/jQueryFileUpload.Net private void UploadPartialFile(string fileName, HttpRequestBase request, List<ImgResultModel> result, ImgTarget e) { if (request.Files.Count != 1) throw new HttpRequestValidationException("Attempt to upload chunked file containing more than one fragment per request"); var file = request.Files[0]; var inputStream = file.InputStream; var fullName = Path.Combine(StorageRoot + getFloderName(e), Path.GetFileName(fileName)); using (var fs = new FileStream(fullName, FileMode.Append, FileAccess.Write)) { var buffer = new byte[1024]; var l = inputStream.Read(buffer, 0, 1024); while (l > 0) { fs.Write(buffer, 0, l); l = inputStream.Read(buffer, 0, 1024); } fs.Flush(); fs.Close(); } result.Add(new ImgResultModel() { FileName = Path.GetFileName(fileName), Type = file.ContentType, Domain = XXW.SiteUtils.SiteAddressHelper.Instance.GetUrl(XXW.Enum.SiteTypeEnum.ResourceSite), path = (getFloderName(e) + "/").Replace("\\", "/"), Size = file.ContentLength }); }
private string save(HttpPostedFileBase file, ImgTarget t, out string extName, bool userOriginalFileName) { string ext = Path.GetExtension(file.FileName); string fn = DateTime.Now.ToString("yyyyMMddHHmmssffff"); if (userOriginalFileName) fn = Path.GetFileNameWithoutExtension(file.FileName) + "_" + DateTime.Now.ToString("yyyyMMddHHmmssffff"); //string fn2 = fn + "_" + DateTime.Now.ToString("yyyyMMddHHmmssdddd"); string a = fn + ext; //var fullPath = Path.Combine(StorageRoot, Path.GetFileName(file.FileName)); string fullPath = Path.Combine(StorageRoot + "\\" + getFloderName(t), a); if (!Directory.Exists(StorageRoot + "\\" + getFloderName(t))) Directory.CreateDirectory(StorageRoot + "\\" + getFloderName(t)); extName = ext; file.SaveAs(fullPath); return a; }
public List<ImgResultModel> UpPic(ImgTarget e, HttpRequestBase request, bool useOriginalFileName) { List<ImgResultModel> r = new List<ImgResultModel>(); foreach (string file in request.Files) { var statuses = new List<ImgResultModel>(); var headers = request.Headers; if (string.IsNullOrEmpty(headers["X-File-Name"])) { UploadWholeFile(request, statuses, e, useOriginalFileName); } else { UploadPartialFile(headers["X-File-Name"], request, statuses, e); } return statuses; } return r; }
public List<ImgResultModel> UpPic(ImgTarget e, HttpRequestBase request) { return UpPic(e, request, false); }
public string getFloderName(ImgTarget t) { string path = string.Empty; switch (t) { case ImgTarget.MemberPhoto: path = "Member\\Photo"; break; case ImgTarget.ReadingBookPic: path = "Reading\\BookPic"; break; case ImgTarget.AcivesPic: path = "Activity\\ActivityPic"; break; case ImgTarget.ActivesAttachment: path = "Activity\\ActivityAtt"; break; case ImgTarget.ReadingRecommendPic: path = "Reading\\Recommend"; break; case ImgTarget.CoursePic: path = "Course\\CoursePic"; break; case ImgTarget.WareContentPic: path = "Course\\WareContentPic"; break; case ImgTarget.WareContentFlash: path = "Course\\WareContentFlash"; break; case ImgTarget.WareAttachment: path = "Course\\WareAttachment"; break; case ImgTarget.PortalEspeciallyPlan: path = "Portal\\EspeciallyPlan"; break; case ImgTarget.PortalEspeciallyPlanPreview: path = "Portal\\EspeciallyPlan"; break; case ImgTarget.PortalEspeciallyPlanHP: path = "Portal\\PortalEspeciallyPlanHP"; break; case ImgTarget.PortalSpecialRecommendCover: path = "Portal\\SpecialRecommendCover"; break; case ImgTarget.CircleCover: path = "Circle\\CircleCover"; break; case ImgTarget.CircleFileAttachment: path = "Circle\\CircleFileAttachment"; break; case ImgTarget.CirclePhotoAttachment: path = "Circle\\CirclePhotoAttachment"; break; case ImgTarget.CourseRollPic: path = "Course\\CourseRollPic"; break; case ImgTarget.SharePic: path = "Share\\SharePic"; break; case ImgTarget.LevelIco: path = "Level\\Ico"; break; case ImgTarget.TeamChargePersonPic: path = "Team\\ChargePersonPic"; break; case ImgTarget.TeamHonorPic: path = "Team\\HonorPic"; break; case ImgTarget.TeamLogo: path = "Team\\Logo"; break; case ImgTarget.RecommendTeamPic: path = "Team\\Recommend"; break; default: path = "Files"; break; } return path + "\\" + TargetPath; }