2013-05-26 166 views
1

我想上傳JPG文件到我在我的項目中創建的文件夾。圖像上傳在Asp.net中的圖像文件夾問題

圖像未保存在圖像文件夾中。它在我上傳時顯示我的圖像,但圖像本身不存在於圖像文件夾中。

這裏是我使用的代碼:

private void btnUpload_Click(object sender, System.EventArgs e) 
{ 
    // Initialize variables 
    string sSavePath; 
    string sThumbExtension; 
    int intThumbWidth; 
    int intThumbHeight; 

    // Set constant values 
    sSavePath = "images/"; 
    sThumbExtension = "_thumb"; 
    intThumbWidth = 160; 
    intThumbHeight = 120; 

    // If file field isn’t empty 
    if (filUpload.PostedFile != null) 
    { 
     // Check file size (mustn’t be 0) 
     HttpPostedFile myFile = filUpload.PostedFile; 
     int nFileLen = myFile.ContentLength; 
     if (nFileLen == 0) 
     { 
      lblOutput.Text = "No file was uploaded."; 
      return; 
     } 

     // Check file extension (must be JPG) 
     if (System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".jpg") 
     { 
      lblOutput.Text = "The file must have an extension of JPG"; 
      return; 
     } 

     // Read file into a data stream 
     byte[] myData = new Byte[nFileLen]; 
     myFile.InputStream.Read(myData,0,nFileLen); 

     // Make sure a duplicate file doesn’t exist. If it does, keep on appending an 
     // incremental numeric until it is unique 
     string sFilename = System.IO.Path.GetFileName(myFile.FileName); 
     int file_append = 0; 
     while (System.IO.File.Exists(Server.MapPath(sSavePath + sFilename))) 
     { 
      file_append++; 
      sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) 
          + file_append.ToString() + ".jpg"; 
     } 

     // Save the stream to disk 
     System.IO.FileStream newFile 
       = new System.IO.FileStream(Server.MapPath(sSavePath + sFilename), 
              System.IO.FileMode.Create); 
     newFile.Write(myData,0, myData.Length); 
     newFile.Close(); 

     // Check whether the file is really a JPEG by opening it 
     System.Drawing.Image.GetThumbnailImageAbort myCallBack = 
         new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback); 
     Bitmap myBitmap; 
     try 
     { 
      myBitmap = new Bitmap(Server.MapPath(sSavePath + sFilename)); 

      // If jpg file is a jpeg, create a thumbnail filename that is unique. 
      file_append = 0; 
      string sThumbFile = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) 
                + sThumbExtension + ".jpg"; 
      while (System.IO.File.Exists(Server.MapPath(sSavePath + sThumbFile))) 
      { 
       file_append++; 
       sThumbFile = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + 
           file_append.ToString() + sThumbExtension + ".jpg"; 
      } 

      // Save thumbnail and output it onto the webpage 
      System.Drawing.Image myThumbnail 
        = myBitmap.GetThumbnailImage(intThumbWidth, 
               intThumbHeight, myCallBack, IntPtr.Zero); 
      myThumbnail.Save (Server.MapPath(sSavePath + sThumbFile)); 
      imgPicture.ImageUrl = sSavePath + sThumbFile; 

      // Displaying success information 
      lblOutput.Text = "File uploaded successfully!"; 

      // Destroy objects 
      myThumbnail.Dispose(); 
      myBitmap.Dispose(); 
     } 
     catch (ArgumentException errArgument) 
     { 
      // The file wasn't a valid jpg file 
      lblOutput.Text = "The file wasn't a valid jpg file."; 
      System.IO.File.Delete(Server.MapPath(sSavePath + sFilename)); 
     } 
    } 
} 

public bool ThumbnailCallback() 
{ 
    return false; 
} 
+0

請您的具體問題,更新您的標題http://meta.stackexchange.com/questions/10647/how-do-i-write-a-good-title –

+0

的原因可能是'Anonymous'登錄或'IISUSRS'沒有寫入你的'images'文件夾的權限。 –

+0

我可以讓那件事發生嗎? –

回答

0

我會感到驚訝,如果它不存在myThumbnail.Save (Server.MapPath(sSavePath + sThumbFile));工作你試圖映射文件中的行...

嘗試

myThumbnail.Save(Server.MapPath(sSavePath) + sThumbFile));

+0

沒有它很好 –

+0

我不知道你是否發現了差異,在我的解決方案中,我先映射沒有文件名的文件夾,然後添加文件名,而將整個路徑映射到文件名 – Ted