public static string SaveUploadeFile(AsyncFileUpload fup, string SaveFolder, UploadFileType filetype, UploadFileSizeLimitation filesizelimit, UploadImageSize? Imagesize = null) { string FileExtension = Path.GetExtension(fup.FileName); if (!IsValidFileType(FileExtension, filetype)) { MessageBoxHelper.ShowMessageBox(fup.Page, "نوع فایل نامعتبر است", "", MessageBoxType.Information); return null; } if (!IsValidFileSize(fup.FileBytes.Length.ToLong(), filesizelimit)) { MessageBoxHelper.ShowMessageBox(fup.Page, "حجم فایل نامعتبر است", "", MessageBoxType.Information); return null; } MemoryStream memStream = Imagesize.HasValue ? ResizeImage(fup.FileBytes, Imagesize.Value, FileExtension) : new MemoryStream(fup.FileBytes); string FileName = Path.GetFileNameWithoutExtension(fup.FileName); if (FileName != null) FileName = FileName.Replace(" ", "_"); FileName = FileName + "__" + Common.CreateTemporaryPassword(8) + FileExtension; String AttchFilePath = HttpContext.Current.Server.MapPath(SaveFolder + FileName); File.WriteAllBytes(AttchFilePath, memStream.ToArray()); return FileName; }
private static bool IsValidFileSize(long fupsize, UploadFileSizeLimitation filesize) { switch (filesize) { case UploadFileSizeLimitation._100K: return fupsize / 1024.0 <= 100; case UploadFileSizeLimitation._200K: return fupsize / 1024.0 <= 200; case UploadFileSizeLimitation._500K: return fupsize / 1024.0 <= 500; case UploadFileSizeLimitation._1M: return fupsize / 1024.0 <= 1000; case UploadFileSizeLimitation._2M: return fupsize / 1024.0 <= 2000; case UploadFileSizeLimitation._5M: return fupsize / 1024.0 <= 5000; case UploadFileSizeLimitation.Unlimited: return fupsize / 1024.0 <= 10000; default: throw new ArgumentOutOfRangeException("filesize"); } }
public static long GetFileSize(UploadFileSizeLimitation filesize) { switch (filesize) { case UploadFileSizeLimitation._100K: return 100 * 1024; case UploadFileSizeLimitation._200K: return 200 * 1024; case UploadFileSizeLimitation._500K: return 500 * 1024; case UploadFileSizeLimitation._1M: return 1000 * 1024; case UploadFileSizeLimitation._2M: return 2000 * 1024; case UploadFileSizeLimitation._5M: return 5000 * 1024; case UploadFileSizeLimitation.Unlimited: return 1000000 * 1024; default: throw new ArgumentOutOfRangeException("filesize"); } }
private static bool IsValidFileSize(FileUpload fup, UploadFileSizeLimitation filesize) { return IsValidFileSize(fup.PostedFile.ContentLength, filesize); }