2010-06-24 45 views
0

當我上傳圖像它顯示此錯誤,請你能幫助我嗎?上傳照片(因爲它正在被另一個進程使用)?

進程無法訪問文件'C:\ Websites \ telugufilmchance \ httpdocs \ User \ photo \ thumb \ PPTS00025sonali.jpeg',因爲它正在被另一個進程使用。 描述:執行當前Web請求期間發生未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。

異常詳細信息:System.IO.IOException:進程無法訪問文件'C:\ Websites \ telugufilmchance \ httpdocs \ User \ photo \ thumb \ PPTS00025sonali.jpeg',因爲它正在被另一個進程使用。

源錯誤:

if (ext == ".JPEG" || ext == ".JPG" || ext == ".PNG" || ext == ".BMP" || ext == ".GIF") 
Line 193:   { 
Line 194:    fupPhoto.PostedFile.SaveAs(path + filename); 
Line 195:    Bitmap src = Bitmap.FromStream(fupPhoto.PostedFile.InputStream) as Bitmap; 
Line 196:    Bitmap result = ResizeBitmap(src); 

這是我的代碼(更新代碼,它是一個編輯頁面,我更新)

if (fupPhoto.FileName != string.Empty) 
     { 
      filename = ran(fupPhoto.FileName); 
      FileInfo fi = new FileInfo(filename); 
      string ext = fi.Extension.ToUpper(); 
      string path = Server.MapPath("../User/photo/thumb/");    
      if (ext == ".JPEG" || ext == ".JPG" || ext == ".PNG" || ext == ".BMP" || ext == ".GIF") 
      { 
       fupPhoto.PostedFile.SaveAs(path + filename); 
       Bitmap src = Bitmap.FromStream(fupPhoto.PostedFile.InputStream) as Bitmap; 
       Bitmap result = ResizeBitmap(src); 
       SizeMgt(filename); 
       System.Drawing.Bitmap img = new System.Drawing.Bitmap(result, newwid, newhgt); 
       System.Drawing.Imaging.ImageCodecInfo jpegcodec = null; 
       System.Drawing.Imaging.EncoderParameters EncParams; 
       foreach (System.Drawing.Imaging.ImageCodecInfo codec in System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()) 
       { 
        if (codec.MimeType == "image/jpeg") 
        { 
         jpegcodec = codec; 
         break; 
        } 
       } 

       EncParams = new System.Drawing.Imaging.EncoderParameters(1); 
       EncParams.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 20L); 
       newfilename = "new" + filename; 
       img.Save(path + newfilename, jpegcodec, EncParams); 
       img.Dispose(); 

      } 
     } 
     else 
     { 
      newfilename = myphot; 
     } 

       cmd = new SqlCommand("sp_uptreg", conn); 
       cmd.CommandType = CommandType.StoredProcedure; 
       cmd.Parameters.AddWithValue("@tregno", lblregno1.Text); 
       cmd.Parameters.AddWithValue("@catid", ddlCategory.SelectedItem.Value); 

       cmd.Parameters.AddWithValue("@fname", txtfname.Text); 
       cmd.Parameters.AddWithValue("@mname", txtmname.Text); 
       cmd.Parameters.AddWithValue("@lname", txtlname.Text); 

       cmd.Parameters.AddWithValue("@emailid", txtEmail.Text); 
       cmd.Parameters.AddWithValue("@phoneno", txtphone.Text); 
       cmd.Parameters.AddWithValue("@mobileno", txtMobile.Text); 
       cmd.Parameters.AddWithValue("@address", txtAdd.Text); 

       cmd.Parameters.AddWithValue("@photos", newfilename); 


        int a = cmd.ExecuteNonQuery(); 

回答

0

我認爲這個函數失敗了,如果文件都準備好了!

fupPhoto.PostedFile.SaveAs(path + filename); 

所以你需要先刪除它(如果存在)。

您可以使用FileInfo(路徑+文件名)來查看FileExist並將其刪除。

從SaveAs中,您會看到具有創建選項。所以如果文件已經準備好,程序就會失敗。

public void SaveAs(string filename) 
{ 
    if (!Path.IsPathRooted(filename) && RuntimeConfig.GetConfig().HttpRuntime.RequireRootedSaveAsPath) 
    { 
     throw new HttpException(SR.GetString("SaveAs_requires_rooted_path", new object[] { filename })); 
    } 
    FileStream s = new FileStream(filename, FileMode.Create); 
    try 
    { 
     this._stream.WriteTo(s); 
     s.Flush(); 
    } 
    finally 
    { 
     s.Close(); 
    } 
} 

我也建議使用關鍵字在需要處置對象使用

 using (Bitmap src = Bitmap.FromStream(fupPhoto.PostedFile.InputStream) as Bitmap) 
     { 
      //Bitmap result = ResizeBitmap(src); 
      //SizeMgt(filename);     
      using (System.Drawing.Bitmap img = new System.Drawing.Bitmap(result, newwid, newhgt)) 
      { 
       System.Drawing.Imaging.ImageCodecInfo jpegcodec = null; 
       System.Drawing.Imaging.EncoderParameters EncParams; 
       foreach (System.Drawing.Imaging.ImageCodecInfo codec in System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()) 
       { 
        if (codec.MimeType == "image/jpeg") 
        { 
         jpegcodec = codec; 
         break; 
        } 
       } 

       EncParams = new System.Drawing.Imaging.EncoderParameters(1); 
       EncParams.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 20L); 
       newfilename = "new" + filename; 
       img.Save(path + newfilename, jpegcodec, EncParams); 
       img.Dispose(); 
      } 
      src.Dispose(); 
     } 
+0

我會檢查並通知你。感謝Aristos先生的回覆 – 2010-06-24 11:37:07

1

我要說的是,你保存文件」不是個碼不要處置你所持有的所有手柄,所以當你試圖再次保存時,它已經被使用了。你可以發佈你的保存代碼嗎?

+0

Mr. Paddy我只是現在粘貼我的代碼請找到一個,謝謝你的回覆 – 2010-06-24 11:03:53

相關問題