2011-03-25 52 views
0

嗨我有一個問題即時上傳圖片到圖片都包含在〜/用戶數據/用戶名/ uploadedimage/image.jpg的陌生的字符串格式+路徑從文件上傳

我的項目路徑我用下面的方法上傳並將圖片的路徑存儲在我的數據庫中。

protected void UploadButton_Click(object sender, EventArgs e) 
    { 

     if (FileUploadControl.HasFile) 
     { 
      try 
      { 
       string theUserId = Session["UserID"].ToString(); 
       OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=root; Password=commando;"); 
       cn.Open(); 
       //string filename = Path.GetFileName(FileUploadControl.FileName); 
       string fileuploadpath = Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/") + Path.GetFileName(FileUploadControl.FileName); 
       FileUploadControl.SaveAs(fileuploadpath); 
       StatusLabel.Text = "Upload status: File uploaded!"; 

       OdbcCommand cmd = new OdbcCommand("INSERT INTO Pictures (UserID, picturepath) VALUES ('" + theUserId + "' , '" + fileuploadpath + "')", cn); 
       cmd.ExecuteNonQuery(); 
      } 

      catch (Exception ex) 
      { 
       StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message; 

      } 

     } 
    } 
} 

我發現什麼是心不是我的項目目錄的路徑,該路徑的一些奇怪的事情:

enter image description here

這裏是我的數據庫的一個片段的第一個idPictures = 1是正確的路徑我需要的名字。

idPictures = 2是fileupload插入到我的數據庫中的那個?

我怎樣才能得到它,因此它會給出一個路徑名這樣的:

~/userdata/2/uploadedimage/batman-for-facebook.jpg 

編輯:

如果我試試這個:

string fileuploadpath = ("~/userdata/"+theUserId+"/uploadedimage/")+Path.GetFileName(FileUploadControl.FileName); 
      FileUploadControl.SaveAs(fileuploadpath); 
      StatusLabel.Text = "Upload status: File uploaded!"; 

      OdbcCommand cmd = new OdbcCommand("INSERT INTO Pictures (UserID, picturepath) VALUES ('"+theUserId+"','"+fileuploadpath+"')", cn); 
      cmd.ExecuteNonQuery(); 
     } 

     catch (Exception ex) 
     { 
      StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message; 

     } 

    } 
} 

    } 

我得到的錯誤:

檔案cou ld不會被上傳。發生以下錯誤:SaveAs方法被配置爲需要一個根路徑,並且路徑'〜/ userdata/1/uploadedimage/holypally.jpg'沒有根。

回答

1

我懷疑它是在您的SQL語句中將反斜槓當作轉義字符。別忘了,您正在使用Server.MpaPath - 即您正在嘗試爲文件找到Windows絕對文件名。

這就是當您不使用參數化SQL語句時發生的情況,而是直接將您本質上用戶指定的文本包含到您的SQL中。 不要這樣做。使用參數化的SQL命令,分別指定值,然後至少不需要擔心不合理的值。

當然,你仍然需要弄清楚真的是是否想存儲翻譯過的路徑,但這是另一回事。

+0

呃,也許這樣的側隙因爲idpictures 2缺少它們,但爲什麼它不像第一個圖片路徑那樣保存,而是它給出了一個完整的路徑 – 2011-03-25 11:57:02

+0

@Garrith:你調用'Server.MapPath'並插入結果作爲'picturepath'的值。你確定你明白'Server.MapPath'的作用嗎? – 2011-03-25 11:58:07

+0

不,我不想要翻譯的路徑和是的,我理解參數化的SQL語句,但ID喜歡先看到,如果我可以得到正確的路徑名稱,然後擔心反斜槓 – 2011-03-25 12:00:28

0

爲了回答我的問題我要創建兩個字符串一個純粹的文件上傳和其他純粹的數據庫路徑存儲:

 string filenameDB = Path.GetFileName(FileUploadControl.FileName); 
     string fileuploadpath = Server.MapPath("~/userdata/"+theUserId+"/uploadedimage/")+Path.GetFileName(FileUploadControl.FileName); 
     FileUploadControl.SaveAs(fileuploadpath); 
     string fileuploadpaths = ("~/userdata/"+theUserId+"/uploadedimage/")+filenameDB; 
     StatusLabel.Text = "Upload status: File uploaded!"; 

     OdbcCommand cmd = new OdbcCommand("INSERT INTO Pictures (UserID, picturepath) VALUES ('"+theUserId+"','"+fileuploadpaths+"')", cn); 
     cmd.ExecuteNonQuery(); 
    } 

    catch (Exception ex) 
    { 
     StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message; 

    } 

} 

}

} 
相關問題