Fileupload returning the same file from several selected files

Asked

Viewed 428 times

1

I have a Fileupload that I select several files and I save in the directory, but I realized that his Filebytes, repeats equally in all 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 shot here:

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

The name, extension, etc always comes right, however Filebytes always gets the size of the first file,.

  • Could you please put all the code?

  • @War-lock You should be searching for the size of the uploadedFile object not?

  • yes, but Upluodfile does not exist fileBytes, only in Fileup, I found strange tbm.

  • I edited............

  • postei, has update panel, but not in fileuplod, Fileupload is in a modal.

1 answer

0


Use the Inputstreamdo uploadedFile object property, according to microsoft documentation this property exposes a Stream of the uploaded file, so we can use a Binaryreader to read the file bytes.

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);
    }
}
  • Size is not an integer?

  • what I want is the bytes of the file

  • this is done using Filebytes, but always returns the same.

  • the way you posted it: Error 63 Cannot implicitly Convert type 'byte' to 'byte[]

  • I tested, but to save. continue saving the same file.

  • @Warlock, Cara I made a mistake, you need the Byte Array, I will check and edit the answer

  • But the problem is not that I have it, but the saving part of the fileupload is saving the same Bytes, where I am setting it doesn’t do any good. The problem is in Fileup.Saveas.

  • @Warlock, I changed the answer using Binaryreader and the Inputstream Voce property gets the byte array from the file.

  • from this: Argument 1: cannot Convert from 'long' to 'int' 57

  • was only put Arq.ARQUIVOBYTE = b.Readbytes(uploadedFile.Contentlentgth); Worked!

  • in case the error "Argument1: cannot Convert from 'long' to 'int' 57" is displayed because Length from Inputstream is a long and Voce needs an integer in the parameter, the way you put it will work as well. Anyway I’ll change the answer to make it clearer.

Show 6 more comments

Browser other questions tagged

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