Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have this code that will get files on client side and slice the file and send it to server (handler) and there it will append the received slices and append them together and save them in a folder.
THIS IS MY QUESTION: what if I want to save the received blobs on server side in a string ? how can I do it?
Thanks

My Handler:

C#
public void ProcessRequest(HttpContext context)
{
    try
    {
        long length = context.Request.ContentLength;
        byte[] buffer = new byte[context.Request.ContentLength];
        context.Request.InputStream.Read(buffer, 0, context.Request.ContentLength);
        string fileName = context.Request.Headers.Get("X_FILE_NAME");
        AppendAllBytes(context.Server.MapPath("~/upload/" + fileName), buffer);
    }
    catch (Exception)
    {

        throw;
    }
}
public static void AppendAllBytes(string path, byte[] bytes)
{
    try
    {

        using (var stream = new FileStream(path, FileMode.Append))
        {
            stream.Write(bytes, 0, bytes.Length);
        }
    }
    catch (Exception)
    {
        throw;
    }
}


My Javascript code (client side)


C#
$(document).ready(function () {
    $("#btnUpload").click(function (evt) {

        var fl = document.getElementById("files");
        var L = fl.files.length;
        var xhr;
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xhr = new XMLHttpRequest();
        }
        else {// code for IE6, IE5
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
        for (var i = 0; i < L ; i++) {
            var file = fl.files[i];
            var blobs = [];
            var start = 0;
            var end = chunkSize;
            var size = file.size;
            var j = chunkSize;
            while (start < size) {
                //push the fragments to an array
                blobs.push(file.slice(start, end));
                start = j;
                j = start + chunkSize;;
                end = start + chunkSize;
                if (end > size) {
                    end = size;
                }
            }
            var fileName = file.name;
            var count = 0;

            while (blob = blobs.shift()) {
                xhr.open('POST', 'FileUploadHandler.ashx', false);
                xhr.setRequestHeader('X_Offset', chunkSize);
                xhr.setRequestHeader('X_Blob_Number', count);
                xhr.setRequestHeader('X_FILE_NAME', fileName);
                xhr.send(blob);
                count++;
            }
        }
    });
});
Posted

1 solution

If what you are being sent is binary data, then you can;t directly store it in a string, as a string is a collection of characters - and converting binary data to characters rarely means you can convert them back again (as not all character codes are valid depending on the character set being used at the time)

You can do it, by converting the binary values to a Base64 string: https://msdn.microsoft.com/en-us/library/dhx0d524(v=vs.110).aspx[^] and using the complementary method to reverse it: https://msdn.microsoft.com/en-us/library/system.convert.frombase64string(v=vs.110).aspx[^] - but you won't be able to read or understand the string.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900