簡體   English   中英

在vb.net中插入文檔名稱和文檔路徑

[英]Insert document name & document path in 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 Reid我嘗試了這個

 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.InvalidCastException”的異常,但未在用戶代碼中處理。其他信息:無法將類型為“ System.Web.UI.WebControls.ListItem”的對象轉換為類型為“ System.Web.HttpPostedFile”的對象。

如果數據集DocumentID為AutoIncrement,則使用列表框代替Label4

 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

這將用於數據表,它將每個PostFile作為新行放入DocDownloads中。 經過更多研究后,由於安全原因,無法訪問完整路徑

您的代碼:

 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM