2013-03-13 110 views
0

我使用Ajax的異步文件上傳到抓取的圖像文件,並將其保存到一個文件夾空文件:ASP.net文件上傳保存

if(file != null && file.hasFile) 
    { 
     if(file is the appropriate file type) 
     { 
     if(the filename is already taken) 
     { 
      add random numbers on the file name until it's unique 
     } 

     file.SaveAs(filepath + filename); 
     } 
    } 

這是我的本地計算機上的偉大工程。但是,當我將程序發佈到服務器時,文件將以正確的名稱保存在正確的位置,但文件大小爲0.我無法打開文件。

我在做什麼錯?

這是我真正的代碼:

 // If cert has a file 
    if(cert != null && cert.HasFile) 
    { 
    // If cert is the appropriate file type 
    if((cert.FileName.IndexOf(".jpg", StringComparison.OrdinalIgnoreCase) < 0) && 
    (cert.FileName.IndexOf(".jpeg", StringComparison.OrdinalIgnoreCase) < 0) && 
    (cert.FileName.IndexOf(".tiff", StringComparison.OrdinalIgnoreCase) < 0) && 
    (cert.FileName.IndexOf(".png", StringComparison.OrdinalIgnoreCase) < 0) && 
    (cert.FileName.IndexOf(".gif", StringComparison.OrdinalIgnoreCase) < 0) && 
    (cert.FileName.IndexOf(".pdf", StringComparison.OrdinalIgnoreCase) < 0)) 
      { 
       // Popup warning 
       Session["imagePopup"] = "true"; 
      } 
      else 
      { 
       string filename = cert.FileName; 

       // If image already exists randomly add numbers until a unique filename is found 
       while(File.Exists(System.Configuration.ConfigurationManager.AppSettings["CertificateSavePath"] + filename)) 
       { 
        Random r = new Random(); 

        filename = filename.Insert(0, r.Next(99).ToString()); 
       } 


       // Save the new file 
       try 
       { 
        // save the file 
        cert.SaveAs(System.Configuration.ConfigurationManager.AppSettings["CertificateSavePath"] + filename); 
       } 
       catch(Exception ex) 
       { 
        error.Text += ex.Message + "<br />"; 
       } 
+0

根據您的僞代碼,一切都很好......所以,如何向我們展示*真正的代碼*呢? – Blachshma 2013-03-13 19:42:02

+1

這聽起來像一個權限問題。你可以仔細檢查你的應用程序在IIS下運行的進程是否有權寫入和修改你要保存的文件夾中的文件? – jadarnel27 2013-03-13 20:17:43

回答

0

我固定它。

我正在運行的程序是這樣的:

我創建了編輯的行的列表視圖。 用戶可以上傳圖像並保存圖像。 當用戶點擊「更新」時,圖像將被保存。

我改變它爲:

在成功的更新事件的圖像立即保存。 單擊「更新」只是刷新頁面。

希望這可以幫助別人