HttpContext receive jQuery parameter

2

I'm having a hard time receiving a parameter from a registration form for an .ashx.

This is a simple form that will receive cadastral data and upload a resume. The upload script I downloaded from the net and is using jQuery / Ajax and C # / HttpHandler.

Follow the codes:

public class file_to_up : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        try
        {
            if (context.Request.QueryString["upload"] != null)
            {
                string pathrefer = context.Request.UrlReferrer.ToString();
                string Serverpath = HttpContext.Current.Server.MapPath("credenciamento\uploads\");

                var postedFile = context.Request.Files[0];

                string file;

                //For IE to get file name
                if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE")
                {
                    string[] files = postedFile.FileName.Split(new char[] { '\' });
                    file = files[files.Length - 1];
                }
                else
                {
                    file = postedFile.FileName;
                }


                if (!Directory.Exists(Serverpath))
                    Directory.CreateDirectory(Serverpath);

                string fileDirectory = Serverpath;
                if (context.Request.QueryString["fileName"] != null)
                {
                    file = context.Request.QueryString["fileName"];
                    if (File.Exists(fileDirectory + "\" + file))
                    {
                        File.Delete(fileDirectory + "\" + file);
                    }
                }

                string ext = Path.GetExtension(fileDirectory + "\" + file);
                file = Guid.NewGuid() + ext;

                fileDirectory = Serverpath + "\" + file;

                postedFile.SaveAs(fileDirectory);

                context.Response.AddHeader("Vary", "Accept");
                try
                {
                    if (context.Request["HTTP_ACCEPT"].Contains("application/json"))
                        context.Response.ContentType = "application/json";
                    else
                        context.Response.ContentType = "text/plain";
                }
                catch
                {
                    context.Response.ContentType = "text/plain";
                }

                context.Response.Write("Success");
            }
        }
        catch (Exception exp)
        {
            context.Response.Write(exp.Message);
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
  }
}

jQuery:

$(function () {
$('#btnFileUpload').fileupload({
    url: 'file-to-up.ashx?upload=start',
    add: function (e, data) {
        console.log('add', data);
        $('#progressbar').show();
        data.submit();
    },
    progress: function (e, data) {
        var progress = parseInt(data.loaded / data.total * 100, 10);
        $('#progressbar div').css('width', progress + '%');
    },
    success: function (response, status) {
        $('#progressbar').hide();
        $('#progressbar div').css('width', '0%');
        console.log('success', response);
        alert('Enviado');
    },
    error: function (error) {
        $('#progressbar').hide();
        $('#progressbar div').css('width', '0%');
        console.log('error', error);
        alert('Ocorreu um erro e o arquivo não pode ser enviado. Tente novamente');
    }
});
});

I would like the name of the file that currently receives a Guid file = Guid.NewGuid() + ext; to receive the candidate's CPF number ... Of course you can question .. Wow .. Guid is perfect, because there will never be the possibility of conflicts .. why then use CPF? First it will not be cumulative the registers and also because I need to link the file to his CPF to be appreciated by the manager who examines the curricula ..

I already lost some hair trying but could not .. Could someone give a help?

    
asked by anonymous 13.08.2015 / 21:04

1 answer

1

Hello,

And if you put

$(function () {
    $('#btnFileUpload').fileupload({
        //incluir o CPF no query string....
        url: 'file-to-up.ashx?upload=start&cpf='+$('#ID_DO_FIELD_CPF').val(),
        //restante do seu código....
    });
});

and in the file that handles the upload, you get the paramter and rename the file ......

//veja que a query string é a mesma do arquivo js....
var cpf= context.Request.QueryString["cpf"];

//com esta variavel vc altera o nome do arquivo...
file = cpf + ext;

Points you should take into account

  • Create a function to remove dots and dashes from cpf
  • Check whether or not it is filled before uploading, this can be a problem, you will force the guy to fill out the CPF before uploading
  • Make the creation check because a guy can give up everything even by sending the file through ajax
  • I hope I have helped ....

        
    02.09.2015 / 23:08