无法分割文件,发送,然后加入服务器 [英] not able to split a file and send and then join in server

查看:114
本文介绍了无法分割文件,发送,然后加入服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从客户端用ajax的javascript,我分成块,并在当所有的块都:收到我加入他们的服务器上传文件。但问题是,即使原始文件和上载的文件是大小相同但两者是不同的。我的意思是GIF文件时,我认为它的不同和相同的视频files.client方code

  VAR XHR =新XMLHtt prequest();

    VAR tempBlob =斑点;
    VAR blobOrFile = tempBlob.slice(fileDataStart,fileDataSent);
    xhr.open('POST','/组合/ UploadBinaryFiles',假);

    xhr.setRequestHeader(缓存控制,无缓存);
    xhr.setRequestHeader(X文件名,文件名);
    xhr.setRequestHeader(X-文件大小,文件大小);
    xhr.setRequestHeader(X-文件-BytesSent,fileDataSent);
    xhr.setRequestHeader(X-文件-SplitCounter,fileSplitCounter);
    xhr.setRequestHeader(内容类型,多部分/表单数据);

    xhr.send(blobOrFile);
 

服务器端code加入

 的FileStream fsSource =新的FileStream(FileOutputPath,FileMode.Append);

    //循环遍历所有的文件夹中的*。第一部分扩展名的文件
    的foreach(FileInfo的fiPart在diSource.GetFiles(@*。部分))
    {
        //创建当前文件的内容的字节数组
        字节[] bytePart = System.IO.File.ReadAllBytes(fiPart.FullName);
        //写字节到重建的文件
        fsSource.Write(bytePart,0,bytePart.Length);
    }
 

要保存分割的文件服务器

  //读取从请求输入流
byte []的缓冲区=新的字节[Request.InputStream.Length]
INT偏移= 0;
INT CNT = 0;
而((CNT = Request.InputStream.Read(缓冲,抵消,10))大于0)
{
    胶印+ = CNT;
}
// 保存存档
使用(的FileStream FS =新的FileStream(fullNameNoExt,FileMode.Create))
{
    fs.Write(缓冲液,0,buffer.Length);
    fs.Flush();
}
 

解决方案

斑点有一些版本的浏览器相关的行为作为记载的 Mozilla开发者网络:斑点。此外,这是它是如何在IE 实施切片方法

这句话的意思是,在新的浏览器片的第二个参数是不长,它是一个结束位置

有一个在这个问题<一href="http://stackoverflow.com/questions/13870127/html5-chunk-and-webworker-does-not-upload-anything">html5块和webworker不上传任何,这应该能派上用场。

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();
}

解决方案

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.

这篇关于无法分割文件,发送,然后加入服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆