2016-12-28 89 views
0

我有文件上傳和上傳鏈接按鈕和提交按鈕..所以當我選擇文件並點擊上傳然後文件名顯示在標籤中,當我再次點擊瀏覽文件和選擇文件並單擊上傳文件,然後重新命名標籤中保存如此意味着標籤即在vb.net中插入文檔名稱和文檔路徑

abc.docx 
def.docx 

多個文件名顯示..等等..

現在我嘗試將這些文件在數據庫中保存不同的記錄表示這些文件保存在數據庫表中,然後看起來像這樣

ID DocumentName DocumentPath 
1 abc.docx /downloads/abc.docx 
2 def.docx /files/def.docx 

爲了這個,我試試這個 SP

 alter procedure spupload_file 
    @DocumentName varchar(100), 
    @Doctype tinyint 
    as 


    insert into DocDownloads (DocumentID,DocumentName,DocType) 

    select (select max(DocumentID) from DocDownloads)+ROW_NUMBER() over(order by @DocumentName),@DocumentName,7 

代碼

 Protected Sub LinkButton1_Click(sender As Object, e As EventArgs) Handles LinkButton1.Click 
     uploadmultiple_file(fileUpEx.FileName) 
    End Sub 

    Public Sub uploadmultiple_file(filename As String) 
     If fileUpEx.HasFiles Then 
      For Each uploadedfile As HttpPostedFile In fileUpEx.PostedFiles 
       'Label4.Text = ("<b>File: </b>" + uploadedfile.FileName) 
       Label4.Text += String.Format("{0}<br />", uploadedfile.FileName) 
      Next 
     End If 
    End Sub 


Protected Sub pb_Add_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles pb_submit.Click 
    Dim strKeyName() As String = {"DocumentName", "DocType"} 
     Dim objKeyVal() As Object = {Label4.Text,7} 
     structDb = objDataSet.ExecSP("tbl", "spupload_file", strKeyName, objKeyVal) 
     If structDb.intCode = 0 Then 
      Label5.Text = structDb.strMessage 
      Label5.CssClass = "error" 
      Exit Sub 
     End If 


    End Sub 

當我嘗試這...這顯示了在數據庫表中的記錄像

1 abc.docx<br />def.docx<br /> 

的地方我想要這樣

1 abc.docx 
2 def.docx 

索引超出了數組的範圍。

以及我如何插入文檔路徑

任何幫助嗎?

根據@Andy裏德 修訂

好,我試試這個

For Each file As HttpPostedFile In ListBox1.Items 
     Dim DocumentName As String = file.FileName 

     Dim strKeyName() As String = {"DocumentName", "DocType"} 
     Dim objKeyVal() As Object = {DocumentName, 7} 
structDb = objDataSet.ExecSP("tbl", "spupload_file", strKeyName, objKeyVal) 

    Next 


    If structDb.intCode = 0 Then 
     Label5.Text = structDb.strMessage 
     Label5.CssClass = "error" 
     Exit Sub 
    End If 

但這顯示錯誤

'' 類型發生在DecibelCRM的異常。 dll,但未在用戶代碼中處理 附加信息:無法轉換類型爲「System.Web.UI.WebControls」的對象。 ListItem'來鍵入'System.Web.HttpPostedFile'。

回答

1

使用列表框,而不是Label4,如果數據集DocumentID是自動遞增

Protected Sub UploadLinkButton_Click(sender As Object, e As EventArgs) Handles UploadLinkButton.Click 
    uploadmultiple_file(FileUpEx.FileName) 

End Sub 

Public Sub uploadmultiple_file(filename As String) 
    If fileUpEx.HasFiles Then 
     'Add each PostedFile to list Box instead of using label 
     For Each uploadedfile As HttpPostedFile In fileUpEx.PostedFiles 
      FilesListBox.Items.Add(uploadedfile.FileName) 
     Next 

    End If 

End Sub 

Protected Sub pb_Add_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubmitButton.Click 
    Dim objDataSet As New objDataSet 'Or whatever you have 
    Dim DocType as Integer = 7 

    'Gets each file from FilesListBox to insert them into objDataSet   
    For Each file As HttpPostedFile In FilesListBox.Items 
     Dim DocumentName as String = file.FileName 

     'Because the DocumentID is AutoIncrement, you don't need to enter it here 
     objDataSet.DocDownloads.AddDocDownloadsRow(DocumentName, DocType) 

    Next 

End Sub 

這將是數據表,它應該將每個張貼文件作爲一個新的行放入DocDownloads中。在做了一些更多的研究後,由於安全原因無法訪問完整路徑

+0

我嘗試你的解決方案,並試圖做與SP,但這顯示錯誤..請檢查我的問題中更新的代碼.. –

+0

'objDataSet.ExecSP()'的代碼是什麼樣子? 在我的答案,我把:'objDataSet.DocDownloads.AddDocDownloadsRow(DocumentName,DocType)'...這應該做你想做的'spupload_file'做的不是嗎?它基本上使用DocumentName和DocType將新行插入到objDataSet的DocDownloads表中。 –

0

您的代碼:

Public Sub uploadmultiple_file(filename As String) 
     If fileUpEx.HasFiles Then 
      For Each uploadedfile As HttpPostedFile In fileUpEx.PostedFiles 
       'Label4.Text = ("<b>File: </b>" + uploadedfile.FileName) 
       Label4.Text += String.Format("{0}<br />", uploadedfile.FileName) 
      Next 
     End If 
    End Sub 

嘗試

Public Sub uploadmultiple_file(filename As String) 

    If fileUpEx.HasFiles Then 
    Try 
     For Each uploadedfile As HttpPostedFile In fileUpEx.PostedFiles      
      Label4.Text += uploadedfile.FileName & vbCRLF 
      'Possibly something like: "Label4.Text += uploadedfile.FullPath & vbCRLF" for the document path 
     Next 

    Catch ex as Exception 
     'Whatever exception handling code  
    End try 

    End If 

End Sub 
+0

這是用於路徑嗎?那麼表格中的數據呢? –