简体   繁体   English

ASPxUploadControl-在服务器端获取上传的文件

[英]ASPxUploadControl - get uploaded file on the server-side

I am using several ASPxUploadControl s in my page. 我在页面中使用了几个ASPxUploadControl I am successfully getting the uploaded files using FileUploadComplete event's e.UploadedFile argument and saving them. 我使用FileUploadComplete事件的e.UploadedFile参数成功获取了上传的文件并保存了它们。

But I want to be able to access (and save) them after saving my main entity. 但是我希望在保存主实体后能够访问(并保存)它们。 The reason is that I need to save them in a folder the name of which will be my entity's ID. 原因是我需要将它们保存在一个文件夹中,该文件夹的名称将是我实体的ID。

Is there a way I can do something like myUploadControl.TheFileItIsCarrying where ever I want in my C# code? 有什么办法可以在my C#代码中的任何我想做的地方执行myUploadControl.TheFileItIsCarrying吗?

I tried myUploadControl.UploadedFiles[0] , but it will return a file with no content. 我尝试了myUploadControl.UploadedFiles[0] ,但是它将返回没有内容的文件。

PS1: I thought of saving them in a temporary folder and then moving to the right folder. PS1:我想将它们保存在一个临时文件夹中,然后再移动到正确的文件夹中。 But in that case, I will not know each file is whose if several clients upload simultaneously. 但是在那种情况下,如果几个客户端同时上传,我将不知道每个文件是谁的。

PS2: The scenario is like saving a number of Party entities along with scanned copies of their identity documents, Resumes, etc. PS2:该场景就像保存多个Party实体以及其身份证件,简历等的扫描副本。

If you are actually saving the uploaded file to the server, you can save the upload file first, then you should be able to do whatever you want with the file, I don't know if you are actually saving it or not but that may be the problem that is occurring. 如果您实际上是将上载的文件保存到服务器,则可以先保存上载的文件,然后您应该可以对文件进行任何操作,我不知道您是否实际保存了文件,但这可能是正在发生的问题。

 If FileUpload1.HasFile Then
        Try
            Dim realPhysicalPath As String = Path.Combine(Server.MapPath("~\Files\"), FileUpload1.PostedFile.FileName)
            FileUpload1.PostedFile.SaveAs(realPhysicalPath)

            Label1.Text = "File name: " & _
               FileUpload1.PostedFile.FileName & "<br>" & _
               "File Size: " & _
               FileUpload1.PostedFile.ContentLength & " kb<br>" & _
               "Content type: " & _
               FileUpload1.PostedFile.ContentType
        Catch ex As Exception
            Label1.Text = "ERROR: " & ex.Message.ToString()
        End Try
    Else
        Label1.Text = "You have not specified a file."
    End If

Then you can pull that specific file from the server, my example is assigning all the saved uploaded files to a GridView 然后您可以从服务器中提取该特定文件,我的示例是将所有保存的上传文件分配给GridView

 Dim filePaths() As String = Directory.GetFiles(Server.MapPath("~/Files/"))
        Dim files As List(Of ListItem) = New List(Of ListItem)
        For Each filePath As String In filePaths

            files.Add(New ListItem(Path.GetFileName(filePath)))
        Next
        Dim hplnk As New HyperLinkField()

        hplnk.DataTextField = "test"
        hplnk.Text = "test"
        hplnk.HeaderText = "yourHeaderTextValue"

        GridView1.DataSource = files



        GridView1.DataBind()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM