Home > Enterprise >  How to pass blob value through ajax call in javascript?
How to pass blob value through ajax call in javascript?

Time:10-30

We are Generating blob value and want to get it inserted into the database through SQL Server. We are able to pass the blob value and through the help of Ajax call we are trying to get it to insert but we are receiving the error Uncaught (in promise) TypeError: Failed to execute 'array buffer on 'Blob': Illegal invocation code. And in WebServices, we are passing it as byte value byte[] BLOB while in SQL we are using var binary(max) to store the value.

async function pdfhandler() {
    let pdfBlob = '1';
    var mystring = "Hello World!";
    pdfBlob = new Blob([mystring]);
    updateblobdata(pdfBlob);
    console.log(pdfBlob);
}

  
function updateblobdata(blobvalue) {
    debugger;
    /// Insert Blob into Database ///
    console.log('updateblobdata called');
    const urlParams = new URLSearchParams(window.location.search);
    const myParamtag = urlParams.get('104');
    const mymodelval = urlParams.get('DuganHosp-0002');

    var wonumval = '104'; var tagnumval = 'DuganHosp-0002';
    var Modeval = 'U';
    var BLOBval = blobvalue;

    $.ajax({

        url: 'TestTool_WebService.asmx/UpdateHtmlBLOBContent',
        data: {
            WoNum: wonumval,
            Tagnum: tagnumval,
            BLOB: BLOBval,
            Mode: Modeval,
        },
        method: 'post',
        dataType: 'json',
        contentType: false,
        processData: false,
        cache: false,
        success: function (data) { },
    });
}

CodePudding user response:

You can send files only with content-type: multipart/form-data.

function updateblobdata(blobvalue) {
    const formData = new FormData();
    formData.append('blob', blobvalue);

    $.ajax({
        url: 'TestTool_WebService.asmx/UpdateHtmlBLOBContent',
        data: formData,
        method: 'post',
        dataType: 'json',
        contentType: 'multipart/form-data',
        processData: false,
        cache: false,
        success: function (data) { },
    });
}

CodePudding user response:

No Still its not working.Its not coming to this point for blob value.

 [WebMethod]
    public void UpdateHtmlBLOBContent(string WoNum, string Tagnum, byte[] BLOB, string Mode)
    {
        string htmldoc = null;
        using (SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["ConStr1"]))
        {

            SqlCommand cmd = new SqlCommand("TestTool_PrototypeprocBlobRun", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Workorder", WoNum);
            cmd.Parameters.AddWithValue("@Equipment", Tagnum);
            cmd.Parameters.AddWithValue("@HTML_blob", BLOB);
            cmd.Parameters.AddWithValue("@MODE", "U");
            con.Open();
            int i = cmd.ExecuteNonQuery();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                htmldoc = "Success";
            }
            dr.Close();

        }

        Context.Response.Write(htmldoc);
    }
}

  • Related