6

I am uploading file from client side using ajax javascript which i split in chunks and in the server when all the chunks are recieved i join them. But the problem is that even though the original file and uploaded file are of same size but both are different. I mean gif files when i view its different and same with video files.client side code

    var xhr = new XMLHttpRequest();

    var tempBlob = blob;
    var blobOrFile = tempBlob.slice(fileDataStart, fileDataSent);
    xhr.open('POST', '/Portfolio/UploadBinaryFiles', false);

    xhr.setRequestHeader("Cache-Control", "no-cache");
    xhr.setRequestHeader("X-File-Name", fileName);
    xhr.setRequestHeader("X-File-Size", fileSize);
    xhr.setRequestHeader("X-File-BytesSent", fileDataSent);
    xhr.setRequestHeader("X-File-SplitCounter", fileSplitCounter);
    xhr.setRequestHeader("Content-Type", "multipart/form-data");

    xhr.send(blobOrFile);

server side code to join

    FileStream fsSource = new FileStream(FileOutputPath, FileMode.Append);

    // Loop through all the files with the *.part extension in the folder
    foreach (FileInfo fiPart in diSource.GetFiles(@"*.part"))
    {
        // Create a byte array of the content of the current file
        Byte[] bytePart = System.IO.File.ReadAllBytes(fiPart.FullName);
        // Write the bytes to the reconstructed file
        fsSource.Write(bytePart, 0, bytePart.Length);
    }

to save split file in server

// Read input stream from request
byte[] buffer = new byte[Request.InputStream.Length];
int offset = 0;
int cnt = 0;
while ((cnt = Request.InputStream.Read(buffer, offset, 10)) > 0)
{
    offset += cnt;
}
// Save file
using (FileStream fs = new FileStream(fullNameNoExt, FileMode.Create))
{
    fs.Write(buffer, 0, buffer.Length);
    fs.Flush();
}
Fals
  • 6,813
  • 4
  • 23
  • 43
manishkr1608
  • 313
  • 1
  • 3
  • 13
  • Is this MVC? WebAPI? WebForms? – Fals Aug 09 '13 at 12:13
  • this is mvc...all server code are in controller which is called through ajax – manishkr1608 Aug 09 '13 at 12:31
  • possible duplicate of [How to do a ASP.NET MVC Ajax form post with multipart/form-data?](http://stackoverflow.com/questions/581703/how-to-do-a-asp-net-mvc-ajax-form-post-with-multipart-form-data) – Fals Aug 09 '13 at 12:53
  • my question is not how to upload...my question is when i join the files in server uploaded file size is exactly same as original file size but still when i view them they are different. – manishkr1608 Aug 09 '13 at 14:49
  • one more thing the same upload works if i dont split and send the whole file in one go. but my problem is i have very big files so something with bit calculation that i think or might be some other issue.m not able to crack this one... – manishkr1608 Aug 09 '13 at 14:54
  • Can you tell us anything additional about the problem data other than _'the image looks different'_ -- have you looked at the actual chunk data or done a binary compare? If the files are the same exact size then there is a good chance that something is going wrong at the boundaries of the buffers. Seeing the data at those points might help with identifying the problem via symptoms. – David Tansey Aug 09 '13 at 18:21
  • @DavidTansey hi...when i compared the files both the files have same no of bytes...i dont know how to compare the binary data... – manishkr1608 Aug 10 '13 at 07:05
  • @user2650513 Try this post for some possibilities for binary compare tools: http://stackoverflow.com/questions/8166697/tool-for-comparing-2-binary-files-in-windows – David Tansey Aug 11 '13 at 16:07
  • @DavidTansey: i ll try to compare binary data of the files and let u know the results...thanks – manishkr1608 Aug 11 '13 at 16:39
  • @DavidTansey: Sorry was down with viral fever so could not work on it...I compared binary files there's difference in the files and the difference starts from the 65536 byte and i am spliting the files in 65536 bytes chunk..so as you said there is issue with the boundary..please suggest what can be done – manishkr1608 Aug 20 '13 at 04:57
  • Are the bytes different for just one or a few or is everything wrong after 65536? Can you show a couple of bytes of OK data and then a couple after it is not OK? Any chance you're seeing zeroes or other repeating data after position 65536? I think the links in the answer posted by @JamieSee probably have what you need. – David Tansey Aug 20 '13 at 05:09
  • @DavidTansey: everything is wrong after 65536. i am not seeing zeroes or other repeating data after position 65536 – manishkr1608 Aug 20 '13 at 05:53
  • @DavidTansey: thanks man ....solved the issue....i was not joining the files in order... – manishkr1608 Aug 20 '13 at 07:23

1 Answers1

0

Blob has some browser version dependent behaviors as documented in Mozilla Developer Network: Blob. Also, this is how it's implemented in IE slice method.

What this means is that in newer browsers slice's second parameter is not a length, it's an end position.

Have a look at this question html5 chunk and webworker does not upload anything, which should prove helpful.

Community
  • 1
  • 1
JamieSee
  • 12,696
  • 2
  • 31
  • 47
  • 1
    solved the issue guys...issue was i was not joining files in order so changed line code foreach (FileInfo fiPart in diSource.GetFiles(@"*.part").OrderBy(f => f.CreationTime)) – manishkr1608 Aug 20 '13 at 07:20