本文整理汇总了C#中System.Web.HttpPostedFile类的典型用法代码示例。如果您正苦于以下问题:C# HttpPostedFile类的具体用法?C# HttpPostedFile怎么用?C# HttpPostedFile使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HttpPostedFile类属于System.Web命名空间,在下文中一共展示了HttpPostedFile类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProcessFile
public override void ProcessFile(HttpPostedFile file)
{
ManualResetEvent e = HttpContext.Current.Items["SyncUploadStorageAdapter"] as ManualResetEvent;
try
{
UploadedFile item = UploadStorageService.CreateFile4Storage(file.ContentType);
item.FileName = Path.GetFileName(file.FileName);
item.ContentLength = file.ContentLength;
item.InputStream = file.InputStream;
if (!string.IsNullOrEmpty(HttpContext.Current.Request.Form["sectionId"]))
{
Section section = Section.Load(new Guid(HttpContext.Current.Request.Form["sectionId"]));
if (section != null)
section.AddFile(item);
}
item.AcceptChanges();
HttpContext.Current.Items["fileId"] = item.ID;
}
finally
{
if (e != null)
{
Trace.WriteLine(string.Format("Signaling event for file in section {0}.", HttpContext.Current.Request.Form["sectionId"]));
e.Set();
}
}
}
示例2: CheckUploadByTwoByte
/// <summary>
/// 根据文件的前2个字节进行判断文件类型 ---说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
/// </summary>
/// <param name="hpf"></param>
/// <param name="code">文件的前2个字节转化出来的小数</param>
/// <returns></returns>
public bool CheckUploadByTwoByte(HttpPostedFile hpf, Int64 code)
{
System.IO.FileStream fs = new System.IO.FileStream(hpf.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
string fileclass = "";
byte buffer;
try
{
buffer = r.ReadByte();
fileclass = buffer.ToString();
buffer = r.ReadByte();
fileclass += buffer.ToString();
}
catch
{
}
r.Close();
fs.Close();
//
//if (fileclass == code.ToString())
//{
// return true;
//}
//else
//{
// return false;
//}
return (fileclass == code.ToString());
}
示例3: GetFileBytes
/// <summary>
/// Gets the file bytes
/// </summary>
/// <param name="context">The context.</param>
/// <param name="uploadedFile">The uploaded file.</param>
/// <returns></returns>
public virtual byte[] GetFileBytes( HttpContext context, HttpPostedFile uploadedFile )
{
// NOTE: GetFileBytes can get overridden by a child class (ImageUploader.ashx.cs for example)
var bytes = new byte[uploadedFile.ContentLength];
uploadedFile.InputStream.Read( bytes, 0, uploadedFile.ContentLength );
return bytes;
}
示例4: AddFileToSession
public void AddFileToSession(string controlId, string filename, HttpPostedFile fileUpload)
{
if (fileUpload == null)
{
throw new ArgumentNullException("fileUpload");
}
else if (controlId == String.Empty)
{
throw new ArgumentNullException("controlId");
}
HttpContext currentContext = null;
if ((currentContext = GetCurrentContext()) != null)
{
var mode = currentContext.Session.Mode;
if (mode != SessionStateMode.InProc) {
#if NET4
throw new InvalidOperationException(Resources_NET4.SessionStateOutOfProcessNotSupported);
#else
throw new InvalidOperationException(Resources.SessionStateOutOfProcessNotSupported);
#endif
}
currentContext.Session.Add(GetFullID(controlId), fileUpload);
}
}
示例5: DescribePostedFile
internal static string DescribePostedFile(HttpPostedFile file)
{
if (file.ContentLength == 0 && string.IsNullOrEmpty(file.FileName))
return "[empty]";
return string.Format("{0} ({1}, {2} bytes)", file.FileName, file.ContentType, file.ContentLength);
}
示例6: UnpackToFile
/// <summary>
/// Unpacks to file.
/// </summary>
/// <param name="args">The arguments.</param>
/// <param name="file">The file.</param>
private static void UnpackToFile(UploadArgs args, HttpPostedFile file)
{
Assert.ArgumentNotNull(args, "args");
Assert.ArgumentNotNull(file, "file");
var filename = FileUtil.MapPath(TempFolder.GetFilename("temp.zip"));
file.SaveAs(filename);
using (var zipReader = new ZipReader(filename))
{
foreach (var zipEntry in zipReader.Entries)
{
var str = FileUtil.MakePath(args.Folder, zipEntry.Name, '\\');
if (zipEntry.IsDirectory)
{
Directory.CreateDirectory(str);
}
else
{
if (!args.Overwrite)
str = FileUtil.GetUniqueFilename(str);
Directory.CreateDirectory(Path.GetDirectoryName(str));
lock (FileUtil.GetFileLock(str))
FileUtil.CreateFile(str, zipEntry.GetStream(), true);
}
}
}
}
示例7: SaveProductMainImage
public static bool SaveProductMainImage(int ProductID, HttpPostedFile OriginalFile, out string[] FileNames)
{
string FileSuffix = Path.GetExtension(OriginalFile.FileName).Substring(1);
bool ProcessResult = false;
FileNames = GetMainImageName(ProductID, FileSuffix);
if (!Directory.Exists(config.PathRoot)) Directory.CreateDirectory(config.PathRoot);
if (config.AllowedFormat.ToLower().Contains(FileSuffix.ToLower()) && config.MaxSize * 1024 >= OriginalFile.ContentLength)
{
ImageHelper ih = new ImageHelper();
ih.LoadImage(OriginalFile.InputStream);
for (int i = 2; i >= 0; i--)
{
if (config.ImageSets[i].Width > 0 && config.ImageSets[i].Height > 0)
{
ih.ScaleImageByFixSize(config.ImageSets[i].Width, config.ImageSets[i].Height, true);
}
ih.SaveImage(config.PathRoot + FileNames[i]);
}
ih.Dispose();
//foreach (string FileName in FileNames)
//{
// //缩小图片为设置尺寸注意图片尺寸与名称对应
// OriginalFile.SaveAs(config.PathRoot + FileName);
//}
ProcessResult = true;
}
return ProcessResult;
}
示例8: AddImage
public ActionResult AddImage(HttpPostedFile uploadFile, int eventID)
{
string[] all = Request.Files.AllKeys;
foreach (string file in Request.Files.AllKeys)
{
HttpPostedFileBase hpf = Request.Files[file];
if (hpf.ContentLength == 0)
continue;
else
{
var fileName = DateTime.Now.Ticks + Path.GetFileName(hpf.FileName);
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Images/uploads/", fileName);
uploadFile.SaveAs(path);
}
}
ViewData["UploadSucceed"] = "Image upload succeded";
return RedirectToAction("ShowImages", new { eventID = eventID });
//if (uploadFile.ContentLength > 0)
//{
// var fileName = DateTime.Now.Ticks + Path.GetFileName(uploadFile.FileName);
// var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Images/uploads/", fileName);
// uploadFile.SaveAs(path);
// ViewData["UploadSucceed"] = "Image upload succeded";
// return RedirectToAction("ShowImages", new { eventID = eventID });
//}
//else
//{
// ViewBag.UploadError = "";
// return View();
//}
}
示例9: PostedFile
public PostedFile(HttpPostedFile httpPostedFile)
{
_fileName = httpPostedFile.FileName;
_stream = httpPostedFile.InputStream;
_contentType = httpPostedFile.ContentType;
_length = httpPostedFile.ContentLength;
}
示例10: MemoryStreamSmall
void MemoryStreamSmall(HttpPostedFile hpFile, string name, string type, int Width, int Height)
{
Image img = Image.FromStream(hpFile.InputStream);
Bitmap bit = new Bitmap(img);
if (bit.Width > Width || bit.Height > Height)
{
int desiredHeight = bit.Height;
int desiredWidth = bit.Width;
double heightRatio = (double)desiredHeight / desiredWidth;
double widthRatio = (double)desiredWidth / desiredHeight;
if (heightRatio > 1)
{
Width = Convert.ToInt32(Height * widthRatio);
}
else
{
Height = Convert.ToInt32(Width * heightRatio);
}
bit = new Bitmap(bit, Width, Height);//图片缩放
}
bit.Save(name.Substring(0, name.LastIndexOf('.')) + "A." + type, System.Drawing.Imaging.ImageFormat.Jpeg);
}
示例11: GenerateVersions
//temp
public bool GenerateVersions(HttpPostedFile file, string leafDirectoryName, string origFileName)
{
Dictionary<string, string> versions = new Dictionary<string, string>();
//Define the version to generate
versions.Add("_thumb", "width=100&height=100&crop=auto&format=jpg"); //Crop to square thumbnail
versions.Add("_medium", "maxwidth=400&maxheight=400format=jpg"); //Fit inside 400x400 area, jpeg
//Loop through each uploaded file
foreach (string fileKey in HttpContext.Current.Request.Files.Keys)
{
if (file.ContentLength <= 0) continue; //Skip unused file controls.
var uploadFolder = GetPhysicalPathForFile(file, leafDirectoryName);
if (!Directory.Exists(uploadFolder)) Directory.CreateDirectory(uploadFolder);
//Generate each version
string[] spFiles = origFileName.Split('.');
foreach (string suffix in versions.Keys)
{
string appndFileName = spFiles[0] + suffix;
string fileName = Path.Combine(uploadFolder, appndFileName);
fileName = ImageBuilder.Current.Build(file, fileName, new ResizeSettings(versions[suffix]), false,
true);
}
}
return true;
}
示例12: SaveAs
protected override void SaveAs(string uploadPath, string fileName, HttpPostedFile file)
{
file.SaveAs(uploadPath + fileName);
ImageTools.MakeThumbnail(uploadPath + fileName, uploadPath + "T32X32_" + fileName, 0x20, 0x20, MakeThumbnailMode.HW, InterpolationMode.High, SmoothingMode.HighQuality);
ImageTools.MakeThumbnail(uploadPath + fileName, uploadPath + "T130X130_" + fileName, 130, 130, MakeThumbnailMode.HW, InterpolationMode.High, SmoothingMode.HighQuality);
ImageTools.MakeThumbnail(uploadPath + fileName, uploadPath + "T300X390_" + fileName, 300, 390, MakeThumbnailMode.Cut, InterpolationMode.High, SmoothingMode.HighQuality);
}
示例13: SavePhoto
public string SavePhoto(HttpPostedFile imageFile)
{
var path = @"~/Images/users_pic";
var filename = string.Format("{0}/{1}", path, imageFile.FileName);
imageFile.SaveAs(System.Web.Hosting.HostingEnvironment.MapPath(filename));
return filename;
}
示例14: SaveAspect
public override ImagePreviewDTO SaveAspect(HttpPostedFile file)
{
ImagePreviewDTO previewDTO = base.SaveAspect(file);
string Folder_60X60 = string.Format("{0}\\60X60", this.FolderServerPath);
string Folder_120X120 = string.Format("{0}\\120X120", this.FolderServerPath);
if (!System.IO.Directory.Exists(Folder_60X60))
{
System.IO.Directory.CreateDirectory(Folder_60X60);
}
if (!System.IO.Directory.Exists(Folder_120X120))
{
System.IO.Directory.CreateDirectory(Folder_120X120);
}
string fileName_60X60 = string.Format(Folder_60X60 + "\\{0}", previewDTO.FileName);
string fileName_120X120 = string.Format(Folder_120X120 + "\\{0}", previewDTO.FileName);
string orignalFilePath = string.Format(this.OrignalImageServerPath + "\\{0}", previewDTO.FileName);
//自己需要使用的尺寸
IWEHAVE.ERP.GAIA.WEBUtil.FileManage.MakeThumbnail(orignalFilePath, fileName_60X60, 60, 60, "AHW");
IWEHAVE.ERP.GAIA.WEBUtil.FileManage.MakeThumbnail(orignalFilePath, fileName_120X120, 120, 120, "AHW");
CompanyImagePreviewDTO companyPreviewDTO = new CompanyImagePreviewDTO(previewDTO);
companyPreviewDTO.Image_60X60 = string.Format("{0}/60X60/{1}", this.FolderPath, previewDTO.FileName);
companyPreviewDTO.Image_120X120 = string.Format("{0}/120X120/{1}", this.FolderPath, previewDTO.FileName);
return companyPreviewDTO;
}
示例15: CreateBllImage
public static byte[] CreateBllImage(HttpPostedFile loadImage)
{
var image = new byte[loadImage.ContentLength];
loadImage.InputStream.Read(image, 0, loadImage.ContentLength);
loadImage.InputStream.Close();
return image;
}