Reputation: 604
This is JQuery part :
$(".btnGorevOlustur").click(function (e) {
var fileUpload = $(".fileGorevResim").get(0);
var files = fileUpload.files;
var dt = new FormData();
for (var i = 0; i < files.length; i++) {
dt.append(files[i].name, files[i]);
}
var gPanoID = id;
var gListeID = gorevListeID;
var gBaslik = $(".txtGorevBaslik").val();
var gAciklama = $(".txtareaGorevAciklama").val();
var gSure = $(".txtGorevSure").val();
dt.append("gpid", gPanoID);
dt.append("glid", gListeID);
dt.append("gbas", gBaslik);
dt.append("gacik", gAciklama);
dt.append("gsur", gSure);
if (gBaslik != null && gBaslik != "" && gAciklama != null && gAciklama != "" && gSure != null && gSure != "") {
$.ajax({
type: "POST",
url: "PanoHandler.ashx",
dataType: "json",
data: dt,
contentType: false,
processData: false,
});
e.preventDefault();
}
});
And this is handler part :
var gorevBaslik = context.Request.Form["gbas"];
var gorevAciklama = context.Request.Form["gacik"];
var gorevSure = context.Request.Form["gsur"];
var gorevPanoID = context.Request.Form["gpid"];
var gorevListeID = context.Request.Form["glid"];
var tarih = DateTime.Now.ToString("ddMMyyyyHHmmss");
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 = context.Server.MapPath("~/img/panofoto/" + file.FileName + tarih);
file.SaveAs(fname);
sgFoto = file.FileName + tarih;
}
}
if (!String.IsNullOrEmpty(gorevBaslik) && !String.IsNullOrEmpty(gorevAciklama) && !String.IsNullOrEmpty(gorevSure))
{
var gorev = new Pano_Gorev
{
Baslik = gorevBaslik,
Aciklama = gorevAciklama,
GorevSuresi = gorevSure,
PanoID = Convert.ToInt32(gorevPanoID),
ListeID = Convert.ToInt32(gorevListeID),
Resim = sgFoto,
Olusturan = 1,
OlusturmaTarihi = DateTime.Now
};
dbo.Pano_Gorev.AddObject(gorev);
dbo.SaveChanges();
sresult = true;
context.Response.Write(sresult);
}
else
{
sresult = false;
context.Response.Write(sresult);
}
The problem is on handler, because all values come as null.
context.request.Form[..] //all coming as null.
I also tried the following :
context.request[..]
But this did not work either.
What should I do to solve this issue?
Upvotes: 2
Views: 1282
Reputation: 462
Use native javascript. For example this is html :
<form id="upload_form" enctype="multipart/form-data" method="post">
<input type="file" name="file1" id="file1"><br>
<input type="button" value="Upload File" onclick="uploadFile()">
</form>
This is javascript :
function uploadFile(){
var file = _("file1").files[0];
// alert(file.name+" | "+file.size+" | "+file.type);
var formdata = new FormData();
formdata.append("file1", file);
var ajax = new XMLHttpRequest();
ajax.open("POST", "url");
ajax.send(formdata);
}
Upvotes: 1
Reputation: 604
I finally found the problem. Jquery document in the project was old version and it was the problem why these codes did not work. When i update the jquery, codes work truely.
I edited some little mistakes.
In Consequence, this code can be usable and problem is solved.
Upvotes: 1