locked
How to save Blob (generated in the client side) to SQL varbinary MAX RRS feed

  • Question

  • User-2015242085 posted

    Hi Folks

    How to save a blob generated in the client side with javascript to SQL (varbinary MAX)

    I got a long string in the client side in a HiddenField value:

    data:audio/wav;base64, ...

    I am getting this error "Implicit conversion from data type nvarchar to varbinary(max) is not allowed. Use the CONVERT function to run this query."

    Any better way to save audio from mobile device web page using recorder.js

    Best Regards

    Wednesday, April 11, 2018 5:29 AM

Answers

  • User-2015242085 posted

    I found a solution! 

    var audd = reader.result;
    audd = audd.replace("data:audio/wav;base64,", "");
    
                        $.ajax({
                            type: 'POST',
                            url: 'save-audio.aspx/UploadAudio',
                            data: '{ "imageData" : "' + audd + '" }',
                            contentType: 'application/json; charset=utf-8',
                            dataType: 'json',
                            success: function (msg) {
                                console.log('Audio Uploaded');
                            }
                        });
    
    
    <system.web.extensions>
            <scripting>
                <webServices>
                    <jsonSerialization maxJsonLength="50000000"/>
                </webServices>
            </scripting>
    </system.web.extensions>

            [WebMethod()]
            public static void UploadAudio(string imageData)
            {
                string Pic_Path = HttpContext.Current.Server.MapPath(Guid.NewGuid().ToString() + ".wav");
                using (FileStream fs = new FileStream(Pic_Path, FileMode.Create))
                {
                    using (BinaryWriter bw = new BinaryWriter(fs))
                    {
                        byte[] data = Convert.FromBase64String(imageData);
                        bw.Write(data);
                        bw.Close();
                    }
                }
            }


    Hope this help others developers to upload the audio file to the server

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 3, 2018 6:54 PM

All replies

  • User1120430333 posted

    Implicit conversion from data type nvarchar to varbinary(max) is not allowed. Use the CONVERT function to run this

    https://docs.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql

    I would say that you will have to convert the nvarchar data to binary data before trying to save it without doing a conversion at the database level.

    Wednesday, April 11, 2018 5:43 AM
  • User-2015242085 posted

    I found a solution! 

    var audd = reader.result;
    audd = audd.replace("data:audio/wav;base64,", "");
    
                        $.ajax({
                            type: 'POST',
                            url: 'save-audio.aspx/UploadAudio',
                            data: '{ "imageData" : "' + audd + '" }',
                            contentType: 'application/json; charset=utf-8',
                            dataType: 'json',
                            success: function (msg) {
                                console.log('Audio Uploaded');
                            }
                        });
    
    
    <system.web.extensions>
            <scripting>
                <webServices>
                    <jsonSerialization maxJsonLength="50000000"/>
                </webServices>
            </scripting>
    </system.web.extensions>

            [WebMethod()]
            public static void UploadAudio(string imageData)
            {
                string Pic_Path = HttpContext.Current.Server.MapPath(Guid.NewGuid().ToString() + ".wav");
                using (FileStream fs = new FileStream(Pic_Path, FileMode.Create))
                {
                    using (BinaryWriter bw = new BinaryWriter(fs))
                    {
                        byte[] data = Convert.FromBase64String(imageData);
                        bw.Write(data);
                        bw.Close();
                    }
                }
            }


    Hope this help others developers to upload the audio file to the server

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 3, 2018 6:54 PM
  • User1120430333 posted

    Probably, no one is going to pay any attention to the answer, since it was not marked as 'answered'. 

    Sunday, June 3, 2018 9:01 PM