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 defaultLote_[ID].[EXTENSÃO]
?– André Ribeiro
@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).
– Maniero
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.
– Igor