Accept any Annex format

Asked

Viewed 55 times

1

I have an application where you can insert attachments and then download them. But I did it in a way that with every file format I want to insert as an attachment, I enter a For Each in 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 way to make the attachment accept any file format without having to create a new one For Each for each one. Is it possible ? Or will I have to keep adding one for for each format ?

  • There is a need to use For Each in that code? It’s not always 1 file with the name following the default Lote_[ID].[EXTENSÃO]?

  • @Andréribeiro is what I had observed. The way it is there is this need, he is operating in only one file, the last one he can find. It is likely that he is testing where there is only one file and then it works, but if this situation changes, it no longer works. I think that the loop is necessary to be able to process all available files or is not necessary because it is guaranteed to have only one file (but these guarantees are complicated, they only work when there are no other bugs).

  • Yeah, it’s always one file. However, for each operation where it is necessary to include the batch of an order, attachments are included, each in its format (depending on which user you choose), all of which are stored in the same folder. That’s how I was able to identify the right file name according to the selected id_batch.

1 answer

3


First of all, that code doesn’t seem to make any sense. I may be wrong because I’m not seeing the whole and I don’t know exactly what your goal is but I think it only works in specific situations.

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

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

I put in the Github for future reference.

Documentation.

So any extension is accepted.

If you really need to specify the list of possible extensions, you will need to add each one individually. If this is necessary you can make only one loop filtering the content.

  • I will test the wildcard. It seems to me that it will work, since all I need is to accept any file format

  • It worked perfectly using . * At first, only xls, xlsx and pdf were allowed. Over time, more extensions were allowed. To avoid error I decided to accept all extensions.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.