2
I’m having a hard time getting a parameter from a registration form to an . ashx.
This is a simple form that will receive registration data and upload a resume. The upload script I downloaded from the net is using jQuery/Ajax and c#/Httphandler.
Follows 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;
received the number of the CPF candidate...
Of course you can question.. Ora.. Guid is perfect, because there will never be the possibility of conflicts.. Why then use CPF?
First that will not be cumulative the registrations and also because I need to link the file to his CPF to be appreciated by the manager examining the resumes..
I’ve lost a few hairs trying but I couldn’t.. Someone could give a help?
your answer did help! This is what you needed. The suggested checks had already been made. Vlw!!
– Abel André