Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript .NET Framework MVC大文件上载_Javascript_C#_Angular_Xmlhttprequest_.net 4.6.1 - Fatal编程技术网

Javascript .NET Framework MVC大文件上载

Javascript .NET Framework MVC大文件上载,javascript,c#,angular,xmlhttprequest,.net-4.6.1,Javascript,C#,Angular,Xmlhttprequest,.net 4.6.1,我正在尝试将大型二进制文件从web客户端上载到.NET4.6.1FrameworkMVCAPI。这些文件的范围从5GB到20GB不等 我曾尝试将文件拆分成块,以上载每个块,并在最后合并结果,但合并的文件始终损坏。如果我使用小文件并且不进行拆分,二进制文件将正常工作。但是,当我拆分和合并文件时,文件已“损坏”。它不会按预期加载或运行 我已经找遍了所有地方,还没有找到一个合适的解决办法,所以我希望有人能在这里帮助我 我遵循了这一点,但我无法让它发挥作用,而且更正后的解决方案从未发布过。在服务器上合并

我正在尝试将大型二进制文件从web客户端上载到.NET4.6.1FrameworkMVCAPI。这些文件的范围从5GB到20GB不等

我曾尝试将文件拆分成块,以上载每个块,并在最后合并结果,但合并的文件始终损坏。如果我使用小文件并且不进行拆分,二进制文件将正常工作。但是,当我拆分和合并文件时,文件已“损坏”。它不会按预期加载或运行

我已经找遍了所有地方,还没有找到一个合适的解决办法,所以我希望有人能在这里帮助我

我遵循了这一点,但我无法让它发挥作用,而且更正后的解决方案从未发布过。在服务器上合并之前,我会跟踪文件的顺序

Javascript(调用uploadData以启动)

函数上传完成(文件){
var formData=new formData();
formData.append('fileName',file.name);
formData.append('completed',true);
var xhr3=新的XMLHttpRequest();
open(“POST”,“api/CompleteUpload”,true);//将块组合在一起
xhr3.send(formData);
返回;
}
函数上载数据(项){
var blob=item.zipFile;
var BYTES_PER_CHUNK=75000000;//示例块大小。
变量大小=blob.SIZE;
//上传内容
var start=0;
var end=每个块的字节数;
var完成=0;
var count=SIZE%BYTES\u PER\u CHUNK==0?SIZE/BYTES\u PER\u CHUNK:Math.floor(SIZE/BYTES\u PER\u CHUNK)+1;
while(开始<大小){
var chunk=blob.slice(开始、结束);
var xhr=new XMLHttpRequest();
xhr.onload=函数(){
已完成=已完成+1;
如果(已完成===计数){
上传完成(项目zipFile);
}
};
xhr.open(“POST”,“/api/MultiUpload”,true);
setRequestHeader(“contentType”,false);
setRequestHeader(“processData”,false);
发送(块);
开始=结束;
结束=开始+每个块的字节数;
}
} 
服务器控制器

    //global vars
    public static List<string> myList = new List<string>();

    [HttpPost]
    [Route("CompleteUpload")]
    public string CompleteUpload()
    {
        var request = HttpContext.Current.Request;

        //verify all parameters were defined

        var form = request.Form;

        string fileName;
        bool completed;
        if (!string.IsNullOrEmpty(request.Form["fileName"]) &&
            !string.IsNullOrEmpty(request.Form["completed"]))
        {
            fileName = request.Form["fileName"];
            completed = bool.Parse(request.Form["completed"]);
        }
        else
        {
            return "Invalid upload request";
        }

        if (completed)
        {
            string path = HttpContext.Current.Server.MapPath("~/Data/uploads/Tamp");
            string newpath = Path.Combine(path, fileName);
            string[] filePaths = Directory.GetFiles(path);

            foreach (string item in myList)
            {
                MergeFiles(newpath, item);
            }
        }

        //Remove all items from list after request is done
        myList.Clear();
        return "success";
    }

    private static void MergeFiles(string file1, string file2)
    {
        FileStream fs1 = null;
        FileStream fs2 = null;
        try
        {
            fs1 = System.IO.File.Open(file1, FileMode.Append);
            fs2 = System.IO.File.Open(file2, FileMode.Open);
            byte[] fs2Content = new byte[fs2.Length];
            fs2.Read(fs2Content, 0, (int)fs2.Length);
            fs1.Write(fs2Content, 0, (int)fs2.Length);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message + " : " + ex.StackTrace + " " + file2);
        }
        finally
        {
            if(fs1 != null) fs1.Close();
            if (fs2 != null)
            {
                fs2.Close();
                System.IO.File.Delete(file2);
            }
        }
    }

    [HttpPost]
    [Route("MultiUpload")]
    public string MultiUpload()
    {
        try
        {                
            var request = HttpContext.Current.Request;
            var chunks = request.InputStream;

            string path = HttpContext.Current.Server.MapPath("~/Data/uploads/Tamp");
            string fileName = Path.GetTempFileName();
            string newpath = Path.Combine(path, fileName);
            myList.Add(newpath);

            using (System.IO.FileStream fs = System.IO.File.Create(newpath))
            {
                byte[] bytes = new byte[77570];

                int bytesRead;
                while ((bytesRead = request.InputStream.Read(bytes, 0, bytes.Length)) > 0)
                {
                    fs.Write(bytes, 0, bytesRead);
                }
            }
            return "test";
        }
        catch (Exception exception)
        {
            return exception.Message;
        }
    }
//全局变量
公共静态列表myList=新列表();
[HttpPost]
[路线(“完成路线”)]
公共字符串CompleteUpload()
{
var request=HttpContext.Current.request;
//验证是否定义了所有参数
var form=request.form;
字符串文件名;
布尔完成;
如果(!string.IsNullOrEmpty(request.Form[“fileName”])&&
!string.IsNullOrEmpty(request.Form[“completed”])
{
fileName=request.Form[“fileName”];
completed=bool.Parse(request.Form[“completed”]);
}
其他的
{
返回“上传请求无效”;
}
如果(已完成)
{
string path=HttpContext.Current.Server.MapPath(“~/Data/uploads/Tamp”);
字符串newpath=Path.Combine(路径,文件名);
string[]filepath=Directory.GetFiles(path);
foreach(myList中的字符串项)
{
合并文件(新路径,项目);
}
}
//请求完成后,从列表中删除所有项目
myList.Clear();
返回“成功”;
}
私有静态无效合并文件(字符串文件1、字符串文件2)
{
FileStream fs1=null;
FileStream fs2=null;
尝试
{
fs1=System.IO.File.Open(file1,FileMode.Append);
fs2=System.IO.File.Open(file2,FileMode.Open);
字节[]fs2Content=新字节[fs2.Length];
fs2.Read(fs2Content,0,(int)fs2.Length);
fs1.Write(fs2Content,0,(int)fs2.Length);
}
捕获(例外情况除外)
{
Console.WriteLine(例如Message+“:“+ex.StackTrace+”+file2);
}
最后
{
如果(fs1!=null)fs1.Close();
如果(fs2!=null)
{
fs2.Close();
System.IO.File.Delete(file2);
}
}
}
[HttpPost]
[路由(“多上传”)]
公共字符串多上传()
{
尝试
{                
var request=HttpContext.Current.request;
var chunks=request.InputStream;
string path=HttpContext.Current.Server.MapPath(“~/Data/uploads/Tamp”);
字符串文件名=Path.GetTempFileName();
字符串newpath=Path.Combine(路径,文件名);
myList.Add(newpath);
使用(System.IO.FileStream fs=System.IO.File.Create(newpath))
{
字节[]字节=新字节[77570];
int字节读取;
而((bytesRead=request.InputStream.Read(bytes,0,bytes.Length))>0)
{
fs.写入(字节,0,字节读取);
}
}
返回“测试”;
}
捕获(异常)
{
返回异常消息;
}
}

您是否尝试过应用Multipart
    //global vars
    public static List<string> myList = new List<string>();

    [HttpPost]
    [Route("CompleteUpload")]
    public string CompleteUpload()
    {
        var request = HttpContext.Current.Request;

        //verify all parameters were defined

        var form = request.Form;

        string fileName;
        bool completed;
        if (!string.IsNullOrEmpty(request.Form["fileName"]) &&
            !string.IsNullOrEmpty(request.Form["completed"]))
        {
            fileName = request.Form["fileName"];
            completed = bool.Parse(request.Form["completed"]);
        }
        else
        {
            return "Invalid upload request";
        }

        if (completed)
        {
            string path = HttpContext.Current.Server.MapPath("~/Data/uploads/Tamp");
            string newpath = Path.Combine(path, fileName);
            string[] filePaths = Directory.GetFiles(path);

            foreach (string item in myList)
            {
                MergeFiles(newpath, item);
            }
        }

        //Remove all items from list after request is done
        myList.Clear();
        return "success";
    }

    private static void MergeFiles(string file1, string file2)
    {
        FileStream fs1 = null;
        FileStream fs2 = null;
        try
        {
            fs1 = System.IO.File.Open(file1, FileMode.Append);
            fs2 = System.IO.File.Open(file2, FileMode.Open);
            byte[] fs2Content = new byte[fs2.Length];
            fs2.Read(fs2Content, 0, (int)fs2.Length);
            fs1.Write(fs2Content, 0, (int)fs2.Length);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message + " : " + ex.StackTrace + " " + file2);
        }
        finally
        {
            if(fs1 != null) fs1.Close();
            if (fs2 != null)
            {
                fs2.Close();
                System.IO.File.Delete(file2);
            }
        }
    }

    [HttpPost]
    [Route("MultiUpload")]
    public string MultiUpload()
    {
        try
        {                
            var request = HttpContext.Current.Request;
            var chunks = request.InputStream;

            string path = HttpContext.Current.Server.MapPath("~/Data/uploads/Tamp");
            string fileName = Path.GetTempFileName();
            string newpath = Path.Combine(path, fileName);
            myList.Add(newpath);

            using (System.IO.FileStream fs = System.IO.File.Create(newpath))
            {
                byte[] bytes = new byte[77570];

                int bytesRead;
                while ((bytesRead = request.InputStream.Read(bytes, 0, bytes.Length)) > 0)
                {
                    fs.Write(bytes, 0, bytesRead);
                }
            }
            return "test";
        }
        catch (Exception exception)
        {
            return exception.Message;
        }
    }