Uploading 2 files via ashx

3

I have the screen below:

Ineedtheusertoselectasongandacover,butI'mnotfindingawaytosendthetwofilesatonceandstillvalidateifMusicandCoverhavebeenselected.

UntilthenIfollowedtheexamplebelow,butJSonlytakesthecontentofthe(upFoto)input,howeverIcreatedthesameexcerptwithdifferentnamestogettheMusicinputupMusica,butJSdoesnotsendanMP3file,anerroroccursinthepushbutton(lbtSend)button.

Wouldanyonehaveasuggestionforthiscase?

$(function(){$('#lbtEnviar').click(function(){varfileUpload=$("#upFoto").get(0);
            var files = fileUpload.files;
            var test = new FormData();
            for (var i = 0; i < files.length; i++) {
                test.append(files[i].name, files[i]);
            }
            $.ajax({
                url: "UploadArquivo.ashx",
                type: "POST",
                contentType: false,
                processData: false,
                data: test,
                // dataType: "json",
                success: function (result) {
                    alert(result);
                },
                error: function (err) {
                    alert(err.statusText);
                }
            });
        });
    })

C # snippet that receives the file:

public void ProcessRequest(HttpContext context)
        {
            try
            {
                if (context.Request.Files.Count > 0)
                {
                    HttpFileCollection files = context.Request.Files;
                    for (int i = 0; i < files.Count; i++)
                    {
                        HttpPostedFile file = files[i];
                        string fname;
                        if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE" || HttpContext.Current.Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER")
                        {
                            string[] testfiles = file.FileName.Split(new char[] { '\' });
                            fname = testfiles[testfiles.Length - 1];
                        }
                        else
                        {
                            fname = file.FileName;
                        }

                        string pathToSave = "D:\Msik_Arquivos" + "\" + fname;
                        file.SaveAs(pathToSave);
                    }
                }
                context.Response.ContentType = "text/plain";
                context.Response.Write("File Uploaded Successfully!");
            }
    
asked by anonymous 24.11.2015 / 00:46

1 answer

0

I opted for another solution without the use of JavaScript, I'm using a FileUpload component and configured the button click to send the file to the folder I needed:

<asp:FileUpload ID="fupFoto" runat="server"/>

<asp:LinkButton ID="lbtEnviar" type="submit" runat="server" OnClick="lbtEnviar_Click">Enviar</asp:LinkButton>

At the click of the submit button I configured the following section:

if (fupMusica.HasFile)
{
  string extensaoMusica = Path.GetExtension(fupMusica.FileName);
  if (extensaoMusica == ".mp3")
  {
    try
    {
      string nomeMusica = fupMusica.FileName;
      string pathToSave = "D:\Musicas_Arquivos" + "\" + nomeMusica;
      fupMusica.SaveAs(pathToSave);
    }
    catch (Exception)
    { // ERRO AO SALVAR }
  }
  else
  { // ESCOLHA UM ARQUIVO MP3 }
}
else
{ // NÃO FOI SELECIONADO NENHUM ARQUIVO }

By default it is only possible to make 4MB of upload, so I had to change the maxAllowedContentLength, executionTimeout, maxRequestLength properties in Web.config.

    
28.11.2015 / 03:45