2011-08-29 94 views
0

我認爲我的上傳在星期五正常工作,但是當我今天上午測試網站時,它無法正常工作。我的上傳應該會進入上傳/文件,然後進入與上傳的產品ID相對應的文件。我上傳的文件沒有進入正確的文件夾

例如:我的測試產品是ProductID 519.我想上傳一個文檔,以便上傳/ 519。當我將鼠標懸停在上傳的文件上時,它顯示uploads/519/PhoneList.xls - 這是正確的。但是,當我在Visual Studio 2010中檢查我的解決方案資源管理器時,文件顯示在519文件之外,因爲519PhoneList.xls

有人可以告訴我爲什麼會發生這種情況,並幫助我弄清楚如何解決它?我試過刪除一個/這裏和那裏,但我找不到合適的地方修復。

Protected Sub SubmitDocument_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SubmitDocument.Click 
    DocumentModal.Hide() 
    'Builds the full absolute URL to be inserted into the database. 
    Dim hostURL As String = Request.Url.Scheme & "://" & Request.Url.Host & ":" & Request.Url.Port & Request.ApplicationPath 
    Dim sqlFileHREF As String = Nothing 
    Dim MarketingTitle As String = DocumentTitle.Text 
    'SQL INSERT: Marketing Table 
    sqlFileHREF = "INSERT INTO Marketing (ProductID, MarketingTypeID, MarketingTitle, MarketingData) VALUES (" & ProductID.Value & " ,4, '" & DocumentTitle.Text & "', '" & hostURL & "uploads/" & ProductID.Value & "/" & DocumentUpload.FileName & "')" 
    sqlFileHREF.Replace("'", "''") 
    DocumentUpload.PostedFile.SaveAs(Server.MapPath("/uploads/" & ProductID.Value & DocumentUpload.PostedFile.FileName)) 
    'Create SQL Connection 
    Dim SqlConnection As New SqlConnection("Server=off-db1;uid=productsDB_admin;pwd=*****;database=Products") 
    SqlConnection.Open() 
    Dim sqlCommand As New SqlCommand(sqlFileHREF, SqlConnection) 
    sqlCommand.ExecuteNonQuery() 
    SqlConnection.Close() 
    Response.Redirect(Request.RawUrl) 
End Sub 


<!-- Add a Document --> 
    <li> 
     <asp:LinkButton ID="DocumentButton" runat="server">Document</asp:LinkButton> 
     <asp:Panel ID="DocumentPanel" runat="server" CssClass="modalPopup" Style="display:none"> 
      Title:<asp:TextBox ID="DocumentTitle" runat="server"></asp:TextBox> 
      <asp:FileUpload ID="DocumentUpload" runat="server" /> 
      <asp:Label ID="DocumentLabel" runat="server"></asp:Label> 
      <asp:Button ID="SubmitDocument" runat="server" Text="Upload" onclick="SubmitDocument_Click" /><asp:Button ID="CancelDocument" runat="server" Text="Cancel" /><asp:HiddenField ID="filename" runat="server" /> 
     </asp:Panel>  
     <asp:ModalPopupExtender ID="DocumentModal" runat="server" DropShadow="True" DynamicServicePath="" Enabled="True" PopupControlID="DocumentPanel" TargetControlID="DocumentButton"></asp:ModalPopupExtender> 
    </li> 
+2

你插入的數據是危險的方式。你應該閱讀關於SQL注入。 – David

+0

謝謝@David,我會的。 – jlg

回答

2

文件夾519是否存在?

DocumentUpload.PostedFile.SaveAs(使用Server.Mappath( 「/上傳/」 & ProductID.Value & DocumentUpload.PostedFile.FileName))

該行有一個錯誤,你就錯過一個& 「/」 productid.value和postingfile.filename之間。

+0

是的,該文件夾存在。我在ProdudctID.Value和DocumentUpload之間添加了&「/」,並且它可以工作。多麼愚蠢的錯誤,多謝一雙眼睛!順便說一句,我必須等5分鐘才能接受你的答案。 – jlg

+0

@David:您應該在引用代碼時使用代碼塊。在眼睛上更容易。 –

1

改變這一行:

Server.MapPath("/uploads/" & ProductID.Value & "/" & DocumentUpload.PostedFile.FileName) 

可能使用的String.Format簡單:

Server.MapPath(String.Format("/uploads/{0}/{1}", ProductId.Value, DocumentUpload.PostedFile.FileName)) 
+0

你能爲我解釋一下嗎?這只是一種不同的寫作方式嗎? – jlg

+0

是的,這只是寫它的一種不同的方式。這樣,您不必使用&符串連接字符串。根據我的愚見,更具可讀性。 –

+0

它和第一個答案一樣好。我希望我能夠檢查兩個答案,因爲他們都是一樣的!謝謝! – jlg