无法拆分文件并发送然后加入服务器

not able to split a file and send and then join in server

本文关键字:然后 服务器 并发 拆分 文件      更新时间:2023-09-26

我使用ajax javascript从客户端上传文件,我将其分成块,当收到所有块时,我在服务器中加入它们。但问题是,即使原始文件和上传的文件大小相同,但两者都不同。我指的是gif文件,当我查看它与视频文件不同和相同时。客户端代码

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

加入的服务器端代码

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

将拆分文件保存在服务器中

// 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有一些依赖于浏览器版本的行为,如Mozilla开发者网络:Blob中所述。此外,这也是它在IE切片方法中的实现方式。

这意味着在较新的浏览器中,slice的第二个参数是而不是长度,它是end position

看看这个问题,html5 chunk和webworker没有上传任何内容,这应该会有所帮助。

相关文章: