插入文件名称& vb.net中的文档路径

时间:2016-12-28 06:37:16

标签: asp.net vb.net file-upload

我有文件上传和上传链接按钮和提交按钮..所以当我选择文件并点击上传然后文件名显示在标签中,当我再次点击浏览文件并选择文件并点击上传然后文件名再次保存在标签中,这意味着在标签中显示多个文件名,即

abc.docx
def.docx 

..等等..

现在我尝试使用不同的记录方法将这些文件保存在数据库中,如果这些文件保存在数据库表中,那么看起来像这样

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

为此我试试这个 属

        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”。

2 个答案:

答案 0 :(得分:1)

如果数据集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

这将是数据表,它应该将每个PostedFile作为新行放入DocDownloads。在进行更多研究后,由于安全原因,无法访问完整路径

答案 1 :(得分: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