2012-06-29 104 views
0

我使用此代碼上傳圖像到server.YES其working.but我需要發送圖像路徑到我的數據庫。我有一個數據庫與配置文件表和ImagePath字段那裏。發送圖像路徑到數據庫

我怎樣才能做到這一點?

我的工作代碼是在這裏

public partial class ProfileDetails2 : System.Web.UI.Page 
    { 
protected void Page_Load(object sender, EventArgs e) 
{ 

} 
protected void upload_Click(object sender, EventArgs e) 
{ 
    int intFileSizeLimit = 10; 
    string strFileNameWithPath = FileUpload1.PostedFile.FileName; 
    string strExtensionName = System.IO.Path.GetExtension(strFileNameWithPath); 
    string strFileName = System.IO.Path.GetFileName(strFileNameWithPath); 
    int intFileSize = FileUpload1.PostedFile.ContentLength/1024; 


    strExtensionName = strExtensionName.ToLower(); 
    if (strExtensionName.Equals(".jpg") || strExtensionName.Equals(".gif")) 
    { 

     if (intFileSize < intFileSizeLimit) 
     { 

      FileUpload1.PostedFile.SaveAs(Server.MapPath("~/images/") + "img" + strExtensionName); 

      lblMessage.Text = "Uploaded file details <hr />" + 
       "File path on your Computer: " + strFileNameWithPath + "<br />" + 
       "File Name: " + strFileName + "<br />" + 
       "File Extension Name: " + strExtensionName + "<br />" + 
       "File Size: " + intFileSize.ToString(); 

     } 
     else 
     { 
      lblMessage.Text = "File size exceeded than limit " + intFileSizeLimit + " KB, Please upload smaller file."; 
     } 
    } 
    else 
    { 
     lblMessage.Text = "Only .jpg or .gif file are allowed, try again!"; 
     lblMessage.ForeColor = System.Drawing.Color.Red; 
    } 
} 

}

+0

什麼,如果擴展名是「.JPG」? – usr

回答

0

可以使用

string strFileNameWithPath = Request.PhysicalApplicationPath + "Uploads\" + FileUpload1.PostedFile.FileName; 

追加路徑這會給你應用程序根路徑中的完整路徑。

我想這就是你要找的。

+0

哦,這是如此不安全。該路徑可能包含「\ .. \」! – usr

+0

@DnshPly我需要發送這個路徑到數據庫field.how我可以做到嗎? –

+0

在理想的情況下,我不認爲這是不安全的,因爲Request.PhysicalApplicationPath返回當前目錄的完整路徑(例如C:\ Inetpub \ wwwroot \ WebAppName \),Uploads是您用來上傳文件的文件夾,然後添加不能包含'..'的張貼文件的文件名,因爲它是不允許的。也許如果使用其他工具如提琴手,螢火蟲,我不能說,但我認爲在這些情況下也是安全的。 – Dinesh

相關問題