@@ -184,15 +184,48 @@ public class JtagController : ControllerBase
else { return TypedResults . InternalServerError ( ret . Error ) ; }
}
/// <summary>
/// 通过Jtag下载比特流文件
/// </summary>
/// <param name="address"> 设备地址 </param>
/// <param name="port"> 设备端口 </param>
/// <param name="file">比特流文件( 最大32MB) </param>
[HttpGet("DownloadBitstream")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async ValueTask < IResult > DownloadBitstream ( string address , int port , IFormFile file )
{
var jtagCtrl = new JtagClient . Jtag ( address , port ) ;
var ret = await jtagCtrl . DownloadBitstream ( ) ;
// 检查文件
if ( file is null | | file . Length < = 0 )
throw new ArgumentException ( "Empty file" , nameof ( file ) ) ;
if ( ret . IsSuccessful ) { return TypedResults . Ok ( ret . Value ) ; }
else { return TypedResults . InternalServerError ( ret . Error ) ; }
// 定义缓冲区大小( 例如: 1MB)
const int bufferSize = 1024 * 1024 ; // 1MB
byte [ ] buffer = new byte [ bufferSize ] ;
long totalBytesRead = 0 ;
// 使用异步流读取文件
using ( var memoryStream = new MemoryStream ( ) )
{
using ( var stream = file . OpenReadStream ( ) )
{
int bytesRead ;
while ( ( bytesRead = await stream . ReadAsync ( buffer , 0 , buffer . Length ) ) > 0 )
{
await memoryStream . WriteAsync ( buffer , 0 , bytesRead ) ;
totalBytesRead + = bytesRead ;
}
}
// 将所有数据转换为字节数组(注意:如果文件非常大,可能不适合完全加载到内存)
byte [ ] fileBytes = memoryStream . ToArray ( ) ;
// 下载比特流
var jtagCtrl = new JtagClient . Jtag ( address , port ) ;
var ret = await jtagCtrl . DownloadBitstream ( fileBytes ) ;
if ( ret . IsSuccessful ) { return TypedResults . Ok ( ret . Value ) ; }
else { return TypedResults . InternalServerError ( ret . Error ) ; }
}
}
}