Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.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
C# 将文件从一个API端点流式传输到另一个API端点_C#_Asp.net Mvc_.net Core - Fatal编程技术网

C# 将文件从一个API端点流式传输到另一个API端点

C# 将文件从一个API端点流式传输到另一个API端点,c#,asp.net-mvc,.net-core,C#,Asp.net Mvc,.net Core,我有一个需要从某个位置下载文件的过程。然后需要将该文件以10 mb的块发布到另一个API端点。在此过程中,无法将文件复制到服务器,必须将其流式传输到另一个端点。我尝试使用以下代码: try { int DefaultReadValue = 10485760; int toRead = 10485760; int position = 0;

我有一个需要从某个位置下载文件的过程。然后需要将该文件以10 mb的块发布到另一个API端点。在此过程中,无法将文件复制到服务器,必须将其流式传输到另一个端点。我尝试使用以下代码:

try
            {

                int DefaultReadValue = 10485760; 
                int toRead = 10485760;
                int position = 0;

                using (HttpClient myClient = new HttpClient())
                {
                    //myClient.DefaultRequestHeaders.TransferEncodingChunked = true;

                    tranfile = await _getVaultFileData.GetVaultFileData(tranfile, token);
                    int fileSize = tranfile.FileSize;
                    using (HttpResponseMessage response = await myClient.GetAsync(tranfile.VaultFileURL, HttpCompletionOption.ResponseHeadersRead))
                    using (Stream readFrom = await response.Content.ReadAsStreamAsync())
                    {
                        readFrom.Position = position;
                        if(readFrom.Position >= fileSize)
                        {
                            fileStatus.Status = "Translation send";
                            fileStatus.Error = "";
                            return fileStatus;
                        }
                        if(fileSize - position < toRead)
                        {
                            toRead = fileSize - position;

                        }
                        int bytesRead;
                        byte[] buffer = new byte[toRead];
                        int offset = 0; 
                        while (toRead > 0 && (bytesRead = readFrom.Read(buffer, offset, toRead)) > 0)
                        {
                            ByteArrayContent content = new ByteArrayContent(buffer);
                            HttpResponseMessage transcriptionPost = await myClient.PostAsync(tranfile.FileTranslationURL, content);
                            toRead -= bytesRead;
                            offset += bytesRead;
                        }
                        toRead = DefaultReadValue;
                    }
                }

            }
试试看
{
int DefaultReadValue=10485760;
int toRead=10485760;
int位置=0;
使用(HttpClient myClient=new HttpClient())
{
//myClient.DefaultRequestHeaders.TransferncodingChunked=true;
tranfile=wait_getVaultFileData.getVaultFileData(tranfile,令牌);
int fileSize=tranfile.fileSize;
使用(HttpResponseMessage response=await myClient.GetAsync(tranfile.VaultFileURL,HttpCompletionOption.ResponseHeadersRead))
使用(Stream readFrom=wait response.Content.ReadAsStreamAsync())
{
readFrom.Position=位置;
if(readFrom.Position>=文件大小)
{
fileStatus.Status=“翻译发送”;
fileStatus.Error=“”;
返回文件状态;
}
如果(文件大小-位置0&(bytesRead=readFrom.Read(缓冲区、偏移量、toRead))>0)
{
ByteArrayContent内容=新的ByteArrayContent(缓冲区);
HttpResponseMessage TransactionPost=等待myClient.PostAsync(tranfile.FileTranslationURL,内容);
探路者-=字节阅读;
偏移量+=字节读取;
}
toRead=DefaultReadValue;
}
}
}
当我排队的时候 readFrom.Position=位置

我得到一个错误: 不支持指定的方法

如果启用TransferncodingChunked,则会出现以下错误:

InnerException={System.InvalidOperationException:“传输编码:分块”标头在未指定内容对象时无法使用。}


我需要做什么才能将流分块读取以发布到另一个端点?

使用而不是
ByteArrayContent
var content=new StreamContent(readFrom)。不要对流使用
using
StreamContent。Dispose
将在完成时关闭它。在任何情况下,都不需要使用
Position
从任何流开始读取。使用文件流
Position=0
将导致浪费操作。网络流只能读取,因此无法设置位置