/// <summary> /// 保存上传的非图片型文件 /// </summary> /// <param name="postedFile"></param> /// <returns></returns> public static Result SaveFile(HttpFile postedFile) { Result errors = new Result(); checkUploadFile(postedFile, errors); if (errors.HasErrors) { logger.Info(errors.ErrorsText); return(errors); } String fileExt = Path.GetExtension(postedFile.FileName); String pathName = PathHelper.Map(sys.Path.DiskPhoto); String fileName = Img.GetFileName(pathName, fileExt); String filenameWithPath = Path.Combine(pathName, fileName); try { postedFile.SaveAs(filenameWithPath); } catch (Exception exception) { logger.Error(lang.get("exPhotoUploadError") + ":" + exception.Message); errors.Add(lang.get("exPhotoUploadErrorTip")); return(errors); } errors.Info = fileName.Replace(@"\", "/"); return(errors); }
private static Result upload_private(String uploadPath, HttpFile postedFile, String picName) { logger.Info("uploadPath:" + uploadPath + ", picName:" + picName); Result result = new Result(); Uploader.checkUploadPic(postedFile, result); if (result.HasErrors) { return(result); } String str = PathHelper.Map(uploadPath); String str2 = picName + "." + Img.GetImageExt(postedFile.ContentType); String srcPath = Path.Combine(str, str2); try { postedFile.SaveAs(srcPath); saveAvatarThumb(srcPath); } catch (Exception exception) { logger.Error(lang.get("exPhotoUploadError") + ":" + exception.Message); result.Add(lang.get("exPhotoUploadErrorTip")); return(result); } // 返回的信息是缩略图 String thumbPath = Img.GetThumbPath(srcPath); result.Info = "face/" + Path.GetFileName(thumbPath); return(result); }
/// <summary> /// 上传图片(自定义保存路径) /// </summary> /// <param name="uploadPath">保存路径(相对路径)</param> /// <param name="postedFile">HttpFile</param> /// <param name="picName">图片名称</param> /// <param name="width">宽度</param> /// <param name="height">高度</param> /// <returns></returns> public static Result SaveImg(String uploadPath, HttpFile postedFile, String picName, int width, int height) { logger.Info("uploadPath : " + uploadPath); logger.Info("picName : " + picName); Result result = new Result(); checkUploadPic(postedFile, result); if (result.HasErrors) { return(result); } String str = PathHelper.Map(uploadPath); String str2 = picName + "." + Img.GetImageExt(postedFile.ContentType); String filename = Path.Combine(str, str2); try { postedFile.SaveAs(filename); Img.SaveThumbnail(filename, Img.GetThumbPath(filename), width, height, SaveThumbnailMode.Cut); } catch (Exception exception) { logger.Error(lang.get("exPhotoUploadError") + ":" + exception.Message); result.Add(lang.get("exPhotoUploadErrorTip")); return(result); } result.Info = Path.GetFileName(Img.GetThumbPath(filename)); return(result); }
/// <summary> /// 保存上传的图片 /// </summary> /// <param name="postedFile"></param> /// <param name="arrThumbType"></param> /// <returns></returns> public static Result SaveImg(HttpFile postedFile, ThumbnailType[] arrThumbType) { Result result = new Result(); checkUploadPic(postedFile, result); if (result.HasErrors) { logger.Info(result.ErrorsText); return(result); } String pathName = PathHelper.Map(sys.Path.DiskPhoto); String photoName = Img.GetPhotoName(pathName, postedFile.ContentType); String filename = Path.Combine(pathName, photoName); try { postedFile.SaveAs(filename); foreach (ThumbnailType ttype in arrThumbType) { saveThumbSmall(filename, ttype); } } catch (Exception exception) { logger.Error(lang.get("exPhotoUploadError") + ":" + exception.Message); result.Add(lang.get("exPhotoUploadErrorTip")); return(result); } result.Info = photoName.Replace(@"\", "/"); return(result); }
static void CometWorker_FileUploadRequested(ref HttpFile file) { string fileName = file.ServerMapPath + "\\Upload\\" + file.FileName; file.SaveAs(fileName); string resourceName = file.FileName.Replace("&", "_"); ResourceManager.AddReplaceResource(fileName, resourceName, ResourceType.Image, file.ClientId); CometWorker.SendToClient(file.ClientId, JSON.Method("ShowImage", resourceName)); }
/// <summary> /// 上传图片(自定义保存路径),同时生成最小的缩略图 /// </summary> /// <param name="uploadPath">保存路径(相对路径)</param> /// <param name="postedFile">HttpFile</param> /// <param name="picName">图片名称</param> /// <param name="width">宽度</param> /// <param name="height">高度</param> /// <returns></returns> public static Result SaveImg(String uploadPath, HttpFile postedFile, String picName, int width, int height, SaveThumbnailMode mode) { logger.Info("uploadPath : " + uploadPath); logger.Info("picName : " + picName); Result result = new Result(); CheckUploadPic(postedFile, result); if (result.HasErrors) { return(result); } String str = PathHelper.Map(uploadPath); String str2 = picName + "." + Img.GetImageExt(postedFile.ContentType); String filename = Path.Combine(str, str2); try { String oldFile = null; if (file.Exists(filename)) { oldFile = filename + "." + Guid.NewGuid() + Path.GetExtension(filename); file.Move(filename, oldFile); } postedFile.SaveAs(filename); try { saveThumbImagePrivate(filename, ThumbnailType.Small, width, height, mode); if (strUtil.HasText(oldFile)) { file.Delete(oldFile); } } catch (OutOfMemoryException ex) { file.Delete(filename); if (strUtil.HasText(oldFile)) { file.Move(oldFile, filename); } String msg = "file format error: " + picName; logger.Error(msg); result.Add(msg); return(result); } } catch (Exception exception) { logger.Error(lang.get("exPhotoUploadError") + ":" + exception.Message); result.Add(lang.get("exPhotoUploadErrorTip")); return(result); } result.Info = Path.GetFileName(Img.GetThumbPath(filename)); return(result); }
/// <summary> /// 保存上传的图片 /// </summary> /// <param name="postedFile"></param> /// <param name="arrThumbType"></param> /// <returns></returns> public static Result SaveImg(HttpFile postedFile, Dictionary <String, ThumbInfo> arrThumbType) { Result result = new Result(); CheckUploadPic(postedFile, result); if (result.HasErrors) { logger.Info(result.ErrorsText); return(result); } String pathName = PathHelper.Map(sys.Path.DiskPhoto); String photoName = Img.GetPhotoName(pathName, postedFile.ContentType); String filename = Path.Combine(pathName, photoName); try { postedFile.SaveAs(filename); foreach (KeyValuePair <String, ThumbInfo> kv in arrThumbType) { Boolean isValid = SaveThumbSingle(filename, kv.Key, kv.Value); if (!isValid) { file.Delete(filename); result.Add("format error: " + postedFile.FileName); return(result); } } } catch (Exception exception) { logger.Error(lang.get("exPhotoUploadError") + ":" + exception.Message); result.Add(lang.get("exPhotoUploadErrorTip")); return(result); } result.Info = photoName.Replace(@"\", "/"); return(result); }
/// <summary> /// 上传图片(自定义保存路径) /// </summary> /// <param name="uploadPath">保存路径(相对路径)</param> /// <param name="postedFile">HttpFile</param> /// <param name="picName">图片名称</param> /// <param name="width">宽度</param> /// <param name="height">高度</param> /// <returns></returns> public static Result SaveImg( String uploadPath, HttpFile postedFile, String picName, int width, int height ) { logger.Info( "uploadPath : " + uploadPath ); logger.Info( "picName : " + picName ); Result result = new Result(); checkUploadPic( postedFile, result ); if (result.HasErrors) return result; String str = PathHelper.Map( uploadPath ); String str2 = picName + "." + Img.GetImageExt( postedFile.ContentType ); String filename = Path.Combine( str, str2 ); try { postedFile.SaveAs( filename ); Img.SaveThumbnail( filename, Img.GetThumbPath( filename ), width, height, SaveThumbnailMode.Cut ); } catch (Exception exception) { logger.Error( lang.get( "exPhotoUploadError" ) + ":" + exception.Message ); result.Add( lang.get( "exPhotoUploadErrorTip" ) ); return result; } result.Info = Path.GetFileName( Img.GetThumbPath( filename ) ); return result; }
/// <summary> /// 保存上传的图片 /// </summary> /// <param name="postedFile"></param> /// <param name="arrThumbType"></param> /// <returns></returns> public static Result SaveImg( HttpFile postedFile, ThumbnailType[] arrThumbType ) { Result result = new Result(); checkUploadPic( postedFile, result ); if (result.HasErrors) { logger.Info( result.ErrorsText ); return result; } String pathName = PathHelper.Map( sys.Path.DiskPhoto ); String photoName = Img.GetPhotoName( pathName, postedFile.ContentType ); String filename = Path.Combine( pathName, photoName ); try { postedFile.SaveAs( filename ); foreach (ThumbnailType ttype in arrThumbType) { saveThumbSmall( filename, ttype ); } } catch (Exception exception) { logger.Error( lang.get( "exPhotoUploadError" ) + ":" + exception.Message ); result.Add( lang.get( "exPhotoUploadErrorTip" ) ); return result; } result.Info = photoName.Replace( @"\", "/" ); return result; }
/// <summary> /// 保存上传的非图片型文件 /// </summary> /// <param name="postedFile"></param> /// <returns></returns> public static Result SaveFile( HttpFile postedFile ) { Result errors = new Result(); checkUploadFile( postedFile, errors ); if (errors.HasErrors) { logger.Info( errors.ErrorsText ); return errors; } String fileExt = Path.GetExtension( postedFile.FileName ); String pathName = PathHelper.Map( sys.Path.DiskPhoto ); String fileName = Img.GetFileName( pathName, fileExt ); String filenameWithPath = Path.Combine( pathName, fileName ); try { postedFile.SaveAs( filenameWithPath ); } catch (Exception exception) { logger.Error( lang.get( "exPhotoUploadError" ) + ":" + exception.Message ); errors.Add( lang.get( "exPhotoUploadErrorTip" ) ); return errors; } errors.Info = fileName.Replace( @"\", "/" ); return errors; }
/// <summary> /// 上传图片(自定义保存路径),同时生成最小的缩略图 /// </summary> /// <param name="uploadPath">保存路径(相对路径)</param> /// <param name="postedFile">HttpFile</param> /// <param name="picName">图片名称</param> /// <param name="width">宽度</param> /// <param name="height">高度</param> /// <returns></returns> public static Result SaveImg( String uploadPath, HttpFile postedFile, String picName, int width, int height, SaveThumbnailMode mode ) { logger.Info( "uploadPath : " + uploadPath ); logger.Info( "picName : " + picName ); Result result = new Result(); CheckUploadPic( postedFile, result ); if (result.HasErrors) return result; String str = PathHelper.Map( uploadPath ); String str2 = picName + "." + Img.GetImageExt( postedFile.ContentType ); String filename = Path.Combine( str, str2 ); try { String oldFile = null; if (file.Exists( filename )) { oldFile = filename + "." + Guid.NewGuid() + Path.GetExtension( filename ); file.Move( filename, oldFile ); } postedFile.SaveAs( filename ); try { saveThumbImagePrivate( filename, ThumbnailType.Small, width, height, mode ); if (strUtil.HasText( oldFile )) { file.Delete( oldFile ); } } catch (OutOfMemoryException ex) { file.Delete( filename ); if (strUtil.HasText( oldFile )) { file.Move( oldFile, filename ); } String msg = "file format error: " + picName; logger.Error( msg ); result.Add( msg ); return result; } } catch (Exception exception) { logger.Error( lang.get( "exPhotoUploadError" ) + ":" + exception.Message ); result.Add( lang.get( "exPhotoUploadErrorTip" ) ); return result; } result.Info = Path.GetFileName( Img.GetThumbPath( filename ) ); return result; }
/// <summary> /// 保存上传的图片 /// </summary> /// <param name="postedFile"></param> /// <param name="arrThumbType"></param> /// <returns></returns> public static Result SaveImg( HttpFile postedFile, Dictionary<String, ThumbInfo> arrThumbType ) { Result result = new Result(); CheckUploadPic( postedFile, result ); if (result.HasErrors) { logger.Info( result.ErrorsText ); return result; } String pathName = PathHelper.Map( sys.Path.DiskPhoto ); String photoName = Img.GetPhotoName( pathName, postedFile.ContentType ); String filename = Path.Combine( pathName, photoName ); try { postedFile.SaveAs( filename ); foreach (KeyValuePair<String, ThumbInfo> kv in arrThumbType) { Boolean isValid = SaveThumbSingle( filename, kv.Key, kv.Value ); if (!isValid) { file.Delete( filename ); result.Add( "format error: " + postedFile.FileName ); return result; } } } catch (Exception exception) { logger.Error( lang.get( "exPhotoUploadError" ) + ":" + exception.Message ); result.Add( lang.get( "exPhotoUploadErrorTip" ) ); return result; } result.Info = photoName.Replace( @"\", "/" ); return result; }
private static Result upload_private( String uploadPath, HttpFile postedFile, String picName ) { logger.Info( "uploadPath:" + uploadPath + ", picName:" + picName ); Result result = new Result(); Uploader.checkUploadPic( postedFile, result ); if (result.HasErrors) return result; String str = PathHelper.Map( uploadPath ); String str2 = picName + "." + Img.GetImageExt( postedFile.ContentType ); String srcPath = Path.Combine( str, str2 ); try { postedFile.SaveAs( srcPath ); saveAvatarThumb( srcPath ); } catch (Exception exception) { logger.Error( lang.get( "exPhotoUploadError" ) + ":" + exception.Message ); result.Add( lang.get( "exPhotoUploadErrorTip" ) ); return result; } // 返回的信息是缩略图 String thumbPath = Img.GetThumbPath( srcPath ); result.Info = "face/" + Path.GetFileName( thumbPath ); return result; }