Fileupload returning the same file from several selected

1

I have a Fileupload that I select several files and I save it in the directory, but I noticed that FileBytes of it, it also repeats in all the files.

if (FileUp.HasFiles)
            {
                foreach (HttpPostedFile uploadedFile in FileUp.PostedFiles)
                {

                    Arquivo arq = new Arquivo();
                    ArquivoVersao arqVersao = new ArquivoVersao();
                    //seta as propriedades de arquivo

                    arq.XARQUIVO = Path.GetFileName(uploadedFile.FileName);
                    arq.EXTENSAO = Path.GetExtension(uploadedFile.FileName);

                      arq.ARQUIVOBYTE = FileUp.FileBytes;
                    FileUp.SaveAs(Server.MapPath("~/db_arquivos/") + arq.XARQUIVO);
    } 
  }

aspx:

 <div class="modal fade open" id="myModal" role="dialog" aria-labelledby="myModalLabel">
                        <div class="modal-dialog">

                            <div class="modal-content">
                                <div class="modal-header" >
                                    <button type="button" class="close" data-dismiss="modal"  aria-hidden="true">&times;</button>
                                    <h4 class="modal-title">
                                        <asp:Label ID="lblModalTitle" runat="server" Text="Enviar arquivos"></asp:Label></h4>
                                </div>
                                <div class="modal-body">
                                    <asp:FileUpload ID="FileUp" AllowMultiple="true" runat="server" />
                                    <br />

                             <input type="text" class="form-control" id="tokenfield" value="red,green,blue" />



                                </div>
                                <div class="modal-footer">
                                    <asp:Button runat="server" CssClass="btn ui-icon-cancel small" ID="UpluodButton" OnClick="UploadButton_Click" Text="Enviar" />

                                </div>
                            </div>

                        </div>
                    </div>

My fileUpload is fired here:

  ScriptManager.RegisterStartupScript(Page, Page.GetType(), "myModal", "$('#myModal').modal();", true);

The name, extension, etc. always comes right, but FileBytes always gets the size of the first file, summarizing the file is saved equally.

    
asked by anonymous 23.03.2015 / 15:05

1 answer

0

Use the InputStream property of the uploadedFile object, according to Microsoft documentation this property exposes a Stream of the uploaded file, so we can use a BinaryReader to read the bytes of the file.

if (FileUp.HasFiles)
{
    foreach (HttpPostedFile uploadedFile in FileUp.PostedFiles)
    {
        Arquivo arq = new Arquivo();
        ArquivoVersao arqVersao = new ArquivoVersao();

        //seta as propriedades de arquivo
        arq.XARQUIVO = Path.GetFileName(uploadedFile.FileName);
        arq.EXTENSAO = Path.GetExtension(uploadedFile.FileName);

        // Utilizando o BinaryReader para obter os bytes do objeto HttpPostedFile
        BinaryReader b = new BinaryReader(uploadedFile.InputStream);
        arq.ARQUIVOBYTE = b.ReadBytes(uploadedFile.ContentLength);

        FileUp.SaveAs(Server.MapPath("~/db_arquivos/") + arq.XARQUIVO);
    }
}
    
23.03.2015 / 15:45