C# C语言中的异步分块文件上载#

C# C语言中的异步分块文件上载#,c#,.net,file-upload,C#,.net,File Upload,我正在尝试使用C#中的HttpClient将文件块从.Net客户端异步上传到php web服务器 我可以很好地将文件分块,并将这些分块上传到远程服务器,但我不确定这是否真的是异步运行的。理想情况下,我会并行上传块,以最大限度地提高上传速度。我的代码如下: //Call to chunk and upload file in another async method. I'm only showing the call here: FileStream fileStream

我正在尝试使用C#中的HttpClient将文件块从.Net客户端异步上传到php web服务器

我可以很好地将文件分块,并将这些分块上传到远程服务器,但我不确定这是否真的是异步运行的。理想情况下,我会并行上传块,以最大限度地提高上传速度。我的代码如下:

//Call to chunk and upload file in another async method. I'm only showing the call here:
            FileStream fileStream = new FileStream(fileNameIn, FileMode.Open, FileAccess.Read);
            await ChunkFileAsync(fileStream, uploadFile.Name, url);

// To chunk the file
        public static async Task<bool> ChunkFileAsync(FileStream fileStream, string fileName, string url)
        {
            int chunkSize = 102400; // Upload 1mb at a time
            int totalChunks = (int)Math.Ceiling((double)fileStream.Length / chunkSize);
            // Loop through the whole stream and send it chunk by chunk asynchronously;
            bool retVal = true;
            List<Task> tasks = new List<Task>();
           try {
                for (int i = 0; i < totalChunks; i++)
                {
                    int startIndex = i * chunkSize;
                    int endIndex = (int)(startIndex + chunkSize > fileStream.Length ? fileStream.Length : startIndex + chunkSize);
                    int length = endIndex - startIndex;

                    byte[] bytes = new byte[length];
                    fileStream.Read(bytes, 0, bytes.Length);

                    Task task = SendChunkRequest(fileName, url, i, bytes);
                    tasks.Add(task);                    
                    retVal = true;
                }
                await Task.WhenAll(tasks);
            }
            catch (WebException e)
            {
                Console.WriteLine("ERROR - There was an error chunking the file before sending the request " + e.Message);
                retVal = false;
            }
            return retVal;

        }

//To upload chunks to remote server
        public static async Task<bool> SendChunkRequest(string fileName, string url, int loopCounter, Byte[] bytes)
        {
            bool response = false;
            try
            {

                ASCIIEncoding ascii = new ASCIIEncoding();
                ByteArrayContent data = new ByteArrayContent(bytes);
                data.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("multipart/form-data");
                byte[] strParamsBytes = ascii.GetBytes(strVals);
                HttpClient requestToServer = new HttpClient();
                MultipartFormDataContent form = new MultipartFormDataContent();
                form.Add(data, "file", fileName + loopCounter);
                await requestToServer.PostAsync(url, form);
                requestToServer.Dispose();
                response = true;

            }
            catch (Exception e)
            {
                Console.WriteLine("There was an exception: " + e);
            }
            return response;
        }
//调用chunk并在另一个异步方法中上载文件。我只是在这里显示呼叫:
FileStream FileStream=newfilestream(fileNameIn,FileMode.Open,FileAccess.Read);
等待ChunkFileAsync(fileStream,uploadFile.Name,url);
//将文件分块
公共静态异步任务ChunkFileAsync(FileStream FileStream、字符串文件名、字符串url)
{
int chunkSize=102400;//一次上载1mb
int totalChunks=(int)Math.天花((double)fileStream.Length/chunkSize);
//循环整个流,并异步地逐块发送;
bool-retVal=true;
列表任务=新列表();
试一试{
for(int i=0;ifileStream.Length?fileStream.Length:startIndex+chunkSize);
int length=endIndex-startIndex;
字节[]字节=新字节[长度];
读取(字节,0,字节,长度);
Task Task=SendChunkRequest(文件名、url、i、字节);
任务。添加(任务);
retVal=true;
}
等待任务。何时(任务);
}
捕获(WebE例外)
{
Console.WriteLine(“错误-在发送请求之前对文件进行分块时出错”+e.Message);
retVal=false;
}
返回返回;
}
//将块上载到远程服务器
公共静态异步任务SendChunkRequest(字符串文件名、字符串url、int loopCounter、字节[]字节)
{
布尔反应=假;
尝试
{
ascienceoding ascii=新的ascienceoding();
ByteArrayContent数据=新的ByteArrayContent(字节);
data.Headers.ContentType=System.Net.Http.Headers.MediaTypeHeaderValue.Parse(“多部分/表单数据”);
字节[]strParamsBytes=ascii.GetBytes(strVals);
HttpClient requestToServer=新HttpClient();
MultipartFormDataContent form=新的MultipartFormDataContent();
添加(数据,“文件”,文件名+循环计数器);
等待requestToServer.PostAsync(url、表单);
requestToServer.Dispose();
响应=真;
}
捕获(例外e)
{
Console.WriteLine(“出现异常:+e);
}
返回响应;
}
如果我上传一个100Mb的文件,我会看到所有十个块一次上传一个到服务器。我能让这个代码更有效吗?非常感谢您的帮助