Sudhansh Pal Sudhansh Pal - 3 months ago 16
Ajax Question

I am trying to upload data to server and data is in blob

I am trying to upload data to server and data is in blob and the problem is when the file is empty it gets upload but it won't if file contain any data in it
following is my code

function uploadData()
{
var param = {
subjectName: $("#ipName").val(),
subjectID: $("#DocSubject").val(),
typeID: $("#DocType").val(),
year: $("#ipDate").val(),
data: dataURL.substr(dataURL.indexOf(',') + 1, dataURL.length),
};
Handler("Json", param, "UploadData");
}
var dataURL;
var handleFiles = function (event) {

var input = event.target;

var reader=new FileReader();
reader.onload=function(){
dataURL=reader.result;
var output=document.getElementById('ipSelect');
output.src=dataURL;
};
reader.readAsDataURL(input.files[0]);

}


Handler is .ashx file which save my data to sql server.I don't know what I am missing.

Answer

FileReader() .readAsDataURL() method returns results asynchronously. You can pass dataURL to uploadData() call at load event of FileReader. Note also, that reader.result:dataURL is a data URI returned from .readAsDataURL(), not a Blob

function uploadData(dataURL) 
{
    var param = {
        subjectName: $("#ipName").val(),
        subjectID: $("#DocSubject").val(),
        typeID: $("#DocType").val(),
        year: $("#ipDate").val(),
        data: dataURL.substr(dataURL.indexOf(',') + 1, dataURL.length)
 };
  Handler("Json", param, "UploadData");
}
var dataURL;
var handleFiles = function (event) {

    var input = event.target;

    var reader=new FileReader();
    reader.onload=function(){
         dataURL=reader.result;
        var output=document.getElementById('ipSelect');
        output.src=dataURL;
        uploadData(dataURL); // call `uploadData()` with `dataURL` as parameter
    };
    reader.readAsDataURL(input.files[0]);

}
Comments