I am uploading files to my app and i’am using ajax & jqyery & handel in asp.net
this my code jqyery
$(document).ready(function ()
{
$('#Button1').click(function ()
{
var files = $('#FileUpload1')[0].files;
if (files.length > 0) {
var id = 1;
var formData = new FormData();
for (var i = 0; i < files.length; i++) {
formData.append(files[i].name, files[i]);
}
$.ajax({
url: 'Handler1.ashx',
method: 'Post',
data: formData,
contentType: false,
processData: false,
success: function () {
alert('success');
},
error: function (err) {
alert(err.error)
}
});
}
});
});
and this my code in handler c#
public void ProcessRequest(HttpContext context)
{
if (context.Request.Files.Count > 0)
{
HttpFileCollection files = context.Request.Files;
for (int i = 0; i < files.Count; i++)
{
HttpPostedFile file = files[i];
string fileName = context.Server.MapPath("~/Uploads/" + System.IO.Path.GetFileName(file.FileName));
file.SaveAs(fileName);
}
}
}
My problem is i need to pass a other variable like id from my jquery to handler someone have any suggestion please
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Just do the FormData.Append in your javascript
formData.append('id', 'test1234');
And in your handler access it via Form
var otherData = context.Request.Form;
Happy coding, cheers!
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0