Accept any attachment format

1

I have an application where you can insert attachments and then download them. But I've done it in a way that for every file format I want to insert as an attachment, I enter a For Each this way

Private Sub grdAnexo_ItemCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles grdAnexo.ItemCommand
        Dim idLote As Integer = Integer.Parse(e.CommandArgument)
        Dim diretorio As New DirectoryInfo(Server.MapPath("Lote_Garantia\"))
        Dim arquivo As FileInfo

        For Each item As FileInfo In diretorio.GetFiles("Lote_" + idLote.ToString + ".xls")
            arquivo = item
        Next

        For Each item As FileInfo In diretorio.GetFiles("Lote_" + idLote.ToString + ".xlsx")
            arquivo = item
        Next

        For Each item As FileInfo In diretorio.GetFiles("Lote_" + idLote.ToString + ".pdf")
            arquivo = item
        Next

        For Each item As FileInfo In diretorio.GetFiles("Lote_" + idLote.ToString + ".xml")
            arquivo = item
        Next

        For Each item As FileInfo In diretorio.GetFiles("Lote_" + idLote.ToString + ".jpg")
            arquivo = item
        Next

        For Each item As FileInfo In diretorio.GetFiles("Lote_" + idLote.ToString + ".jpeg")
            arquivo = item
        Next

        For Each item As FileInfo In diretorio.GetFiles("Lote_" + idLote.ToString + ".bmp")
            arquivo = item
        Next

        For Each item As FileInfo In diretorio.GetFiles("Lote_" + idLote.ToString + ".gif")
            arquivo = item
        Next

        Response.ContentType = "application/octet-stream"
        Response.AddHeader("Content-Disposition", String.Format("attachment;filename=""{0}""", arquivo.Name))
        Response.AddHeader("Content-Length", arquivo.Length.ToString())
        Response.WriteFile(arquivo.FullName)
        Response.End()

    End Sub

My doubt is, a means of having the attachment accept any file format without having to create a new For Each for each one. It's possible ? Or do I have to keep adding for to each format?

    
asked by anonymous 03.02.2015 / 15:39

1 answer

3

First, this code does not seem to make sense. I may be wrong because I am not seeing the whole and I do not know exactly what its purpose is but I think it only works in specific situations.

If you want to handle any type of file, use a wildcard :

diretorio.GetFiles("Lote_" + idLote.ToString + ".*")

So any extension is accepted.

If you really need to specify the list of possible extensions, it is clear that you will have to add each individually. If this is required you can only loop through the content.

    
03.02.2015 / 15:47