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?