2016-12-25 79 views
-1

我需要保存目錄產品的2張照片 - 小和標準。但是,即時通訊使用SavePhoto方法IM當第二次獲得的ArgumentException:用雙內存和文件流寫入2個文件

異常信息:System.ArgumentException:未知參數

問題解決了!對於人如果同樣的問題:

HttpPostedFileBase文件的InputStream應立即使用,這個領域,我們必須修改,以獲得緩衝區爲他人MemoryStream的,所以現在Method的將是1,而不是2

控制器:

[HttpPost] 
public ActionResult UploadProdImages(IEnumerable<HttpPostedFileBase> photos, int pid) 
{     
    if (photos != null) 
    { 
     foreach (var file in photos) 
     {      
      _pRepository.SavePhoto(file, pid); 
    ------> _pRepository.SavePhoto(file, pid,"small"); <----- //Remove this    
     } 
    } 

    return Json(new { pimgid = pid}); 
} 

服務類:

public void SavePhoto(HttpPostedFileBase file, int pid,string t) 
{      
    string dirPath = null; 
    byte[] istream = null; 
    string filePath = null; 
    string filePathSmall = null; 
    string dirPathSmall = null; 

    // file.InputStream using only once! 
    istream = new WebImage(file.InputStream).Resize(1920, 1080,true,true).GetBytes(); 
    //implementing if need Check directory if exist and create if not 
    dirPath = HttpContext.Current.Server.MapPath("~/Content/Files/Product/" + pid);    
    dirPathSmall = HttpContext.Current.Server.MapPath("~/Content/Files/Product/" + pid+"/200x150"); 

    //Full path's with file name 
    filePath = Path.Combine(dirPath, GetRandomName() + Path.GetExtension(file.FileName)); 
    filePathSmall = Path.Combine(dirPathSmall, GetRandomName() + Path.GetExtension(file.FileName)); 
    //Save first time standart size photo 
    using (MemoryStream ms = new MemoryStream(istream)) 
    { 
     using (FileStream fs = new FileStream(filePath, FileMode.Create)) 
     { 
      fs.CopyTo(ms); 
      ms.CopyTo(fs); 
     } 
    } 
    //Save second time small photo with same input - istream 
    istream = new WebImage(istream).Resize(150, 150,false).GetBytes(); 
    using (MemoryStream ms = new MemoryStream(istreamSmall)) 
    { 
     using (FileStream fs = new FileStream(filePathSmall, FileMode.Create)) 
     { 
      fs.CopyTo(ms); 
      ms.CopyTo(fs); 
     } 
    } 
} 

那麼我怎樣才能合併的MemoryStream(具有不同的對象)和的FileStream(不同路徑)?

+0

什麼是例外? – Abion47

+0

在哪一行引發異常? – GeorgeChond

+0

再次查看,更新 –

回答

0

也許我想得簡單,但你是不是傳遞的第三個參數(字符串)到你​​的第一個電話的功能...

+0

第一種方法做得很好,但第二次當我打電話給它,即時獲得例外 –

0

也許是因爲的FileStream自動關閉底層流時被設置的通過使用聲明