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:
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)
$(document).ready(function () {
$("#btnUpload").click(function (evt) {
var fl = document.getElementById("files");
var L = fl.files.length;
var xhr;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
else {
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) {
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++;
}
}
});
});