Hi,

I have two projects; First one is a asp.net web project and the second one is embedded http server library project.

Embedded http server project is taken from : embedded http server project

I want to save a video file from user's local to user's shared storage. I'm getting and sending file from browser using ajax request. Embedded http server is supposed to get bytearray and save video on client's shared storage. I have a problem that I spent days to solve but not yet found a solution.

In chrome it stucks on stream.CopyTo(streamReader);

In firefox and IE it gives "Cross-Origin Request Blocked" error but firefox saves file even it gives the error.

Here is the ajax request code;

$(document).ready(function () {

            var filename='';

            function generateUUID(){
                var d = new Date().getTime();
                var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
                    var r = (d + Math.random()*16)%16 | 0;
                    d = Math.floor(d/16);
                    return (c=='x' ? r : (r&0x3|0x8)).toString(16);
                });
                return uuid;
            };

          function hashFile(file, chunkSize, callback) {
            var size = file.size;
            var offset = 0;
            var chunk = file.slice(offset, offset + chunkSize);

            SendChunk(chunk,0);
            offset += chunkSize;

            var hashChunk = function () {
                var reader = new FileReader();
                reader.onload = function (e) {

                    var offsetCheck = offset + chunkSize;

                    if (offsetCheck < size) {
                        chunk = file.slice(offset, offset + chunkSize);

                        SendChunk(chunk,0);

                        console.log('next send offset:'+offset+" , "+(offset+chunkSize));
                        offset += chunkSize;
                    } 
                    else if (offsetCheck > size){
                        chunk = file.slice(offset, size);

                        SendChunk(chunk,1);

                        console.log('last send offset:'+offset+" , "+size);
                        offset = size;
                    }
                };

                reader.readAsArrayBuffer(chunk);
            };

            function SendChunk(chunk,end){

                if(end>0)
                {
                     var ajaxRequest = $.ajax({
                        type: "POST",
                        url: "http://clientip:8080/savefileend?p="+filename,
                        contentType: false,
                        processData: false,
                        data: chunk
                    });
                }
                else{
                    var ajaxRequest = $.ajax({
                        type: "POST",
                        url: "http://clientip:8080/savefile?p="+filename,
                        contentType: false,
                        processData: false,
                        data: chunk
                    });

                    ajaxRequest.done(function (e) {
                        console.log('done');
                        hashChunk();

                    });
                    ajaxRequest.error(function (xhr) {
                        console.log(xhr);

                        hashChunk();
                    });
                }
            }   
        }

        function fileInputHandler(evt) {
            filename = generateUUID();
            var files = evt.target.files;
            var chunkSize = 10485760; // bytes
            var start = window.performance ? performance.now() : Date.now();

            var onHashFile = function (digest) {
                var end = window.performance ? performance.now() : Date.now(); 
                console.log(this.name, digest, (end - start) + 'ms'); 
            };

            for (var i = 0, len = files.length; i < len; i++) {
                hashFile(files[i], chunkSize, onHashFile);
            }
        }

        document.getElementById('file1')
  .addEventListener('change', fileInputHandler, false);

});

and here is the embedded server code to get the request;

var stream = request.GetRequestStream();

                    using (var streamReader = new MemoryStream())
                    {
                        stream.CopyTo(streamReader);
                        videoTemp = streamReader.ToArray();
                    }

                    using (var fileStream = new FileStream(path, FileMode.Append))
                    {
                        fileStream.Write(videoTemp, 0, videoTemp.Length);
                    }

By the way,

For IE: If I enabled "Access data sources across domains" from setting -security... then it works without error in IE.

For Chrome: If I start chrome with --disable-web-security parameter it works without error in chrome.

But I have find the solution from code.

Need suggestions urgently..

I have found the solution, if anyone needs it;
I have used http://nancyfx.org/ Nancy.Hosting.Self library for embedded http server, Here I was able to add "Access-Control-Allow-Origin" to response.Headers so that I could transfer file without error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.