2009-12-19 20 views

回答

0

海使用這種方法,

using System.IO; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.Text; 
using System.Text.RegularExpressions; 



public string SaveImageFile(FileUpload fu, string directoryPath, int MaxWidth, int   MaxHeight, string prefixName) 
{ 
    string serverPath = "", returnString = ""; 
    if (fu.HasFile) 
    { 
     Byte[] bytes = fu.FileBytes; 
     //Int64 len = 0; 
     prefixName = "Testing" + prefixName; 
     //directoryPath = "Testing/"; 
     System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes); 
     System.Drawing.Image img = System.Drawing.Image.FromStream(ms); 
     string dipath = System.Web.HttpContext.Current.Server.MapPath("~/") + directoryPath; 
     DirectoryInfo di = new DirectoryInfo(dipath); 
     if (!(di.Exists)) 
     { 
      di.Create(); 
     } 
     HttpPostedFile file = fu.PostedFile; 
     DateTime oldTime = new DateTime(1970, 01, 01, 00, 00, 00); 
     DateTime currentTime = DateTime.Now; 
     TimeSpan structTimespan = currentTime - oldTime; 
     prefixName += ((long)structTimespan.TotalMilliseconds).ToString(); 
     if (IsImage(file)) 
     { 
      using (Bitmap bitmap = new Bitmap(file.InputStream, false)) 
      { 
       serverPath = dipath + "//" + prefixName +  fu.FileName.Substring(fu.FileName.IndexOf(".")); 
       img.Save(serverPath); 
       returnString = "~/" + directoryPath + "//" + prefixName + fu.FileName.Substring(fu.FileName.IndexOf(".")); 
      } 
     } 
    } 
    return returnString; 
} 

private bool IsImage(HttpPostedFile file) 
{ 
    if (file != null && Regex.IsMatch(file.ContentType, "image/\\S+") && 
     file.ContentLength > 0) 
    { 
     return true; 
    } 
    return false; 
} 

SaveImageFile方法返回,這將是路徑的字符串...

0

HttpPostedFile具有密封的構造;你可以使用反射來創建HttpPostedFile類。該ctor採取:字符串文件名,字符串contentType,HttpInputStream流

所以你必須通過反射,如果你喜歡的路徑傳遞給HttpPostedFile構造函數。不知道在默認情況下被傳遞到HttpInputStream。