它尝试将excel文件从angular上传到c#async方法并读取它 并将进度返回到角度,直到它完成但它不起作用 但它会在第一次回归中停止
任何修复方法并将我的相关内容返回到角度HTTP帖子
我的c#代码是
[HttpPost("[action]")]
public async Task<IActionResult> UploadFiles(IFormFile files)
{
string hresult = "done";
string message = "uploaded Successfully";
try
{
Stream stream = files.OpenReadStream();
var binaryReader = new BinaryReader(stream);
var fileContent = binaryReader.ReadBytes((int)files.Length);
MemoryStream ms = new MemoryStream(fileContent);
using (ExcelPackage package = new ExcelPackage(ms))
{
StringBuilder sb = new StringBuilder();
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
int rowCount = worksheet.Dimension.Rows;
int ColCount = worksheet.Dimension.Columns;
bool bHeaderRow = true;
int tempCount = 0;
for (int row = 1; row <= rowCount; row++)
{
for (int col = 1; col <= ColCount; col++)
{
if (bHeaderRow)
{
sb.Append(worksheet.Cells[row, col].Value.ToString() + "\t");
}
else
{
sb.Append(worksheet.Cells[row, col].Value.ToString() + "\t");
}
var pertc = tempCount * 100 / rowCount;
tempCount++;
var progresult = new ObjectResult(pertc)
{
StatusCode = (int)HttpStatusCode.OK
};
Request.HttpContext.Response.Headers.Add("X-Total-Count", "Working");
return progresult;
}
sb.Append(Environment.NewLine);
}
}
}
catch (Exception ex)
{
hresult = ex.Message;
}
var result = new ObjectResult(message)
{
StatusCode = (int)HttpStatusCode.OK
};
Request.HttpContext.Response.Headers.Add("X-Total-Count", hresult);
return result;
}
我从像这样的角度调用它
const fileBrowser = this.fileInput.nativeElement;
if (fileBrowser.files && fileBrowser.files[0]) {
const formData = new FormData();
formData.append('files', fileBrowser.files[0]);
this.http.post('/api/Main/UploadFiles', formData)
.subscribe(
res => {
console.log(res);
},
err => {
console.log("Error occured");
}
);
以任何方式从异步c#方法获取百分比以获得角度
答案 0 :(得分:0)
以这种方式使用OkObjectResult Ok
方法return Ok(progresult);
例如:
[HttpPost("[action]")]
public async Task<IActionResult> UploadFiles(IFormFile files)
{
string hresult = "done";
string message = "uploaded Successfully";
try
{
Stream stream = files.OpenReadStream();
var binaryReader = new BinaryReader(stream);
var fileContent = binaryReader.ReadBytes((int)files.Length);
MemoryStream ms = new MemoryStream(fileContent);
using (ExcelPackage package = new ExcelPackage(ms))
{
StringBuilder sb = new StringBuilder();
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
int rowCount = worksheet.Dimension.Rows;
int ColCount = worksheet.Dimension.Columns;
bool bHeaderRow = true;
int tempCount = 0;
for (int row = 1; row <= rowCount; row++)
{
for (int col = 1; col <= ColCount; col++)
{
if (bHeaderRow)
{
sb.Append(worksheet.Cells[row, col].Value.ToString() + "\t");
}
else
{
sb.Append(worksheet.Cells[row, col].Value.ToString() + "\t");
}
var pertc = tempCount * 100 / rowCount;
tempCount++;
var progresult = new ObjectResult(pertc)
{
StatusCode = (int)HttpStatusCode.OK
};
Request.HttpContext.Response.Headers.Add("X-Total-Count", "Working");
return Ok(progresult);
}
sb.Append(Environment.NewLine);
}
}
}
catch (Exception ex)
{
hresult = ex.Message;
}
var result = new ObjectResult(message)
{
StatusCode = (int)HttpStatusCode.OK
};
Request.HttpContext.Response.Headers.Add("X-Total-Count", hresult);
return Ok(result);
}
On Angular .ts文件:
this.http.post('/api/Main/UploadFiles', formData)
.map((res: Response) =>
{
console.log(res);
})
.catch(error =>
{
console.log("Error occured");
});
}
答案 1 :(得分:0)
HTTP调用是不可能的,因为它们每个请求只允许一个响应,并且服务器不知道任何客户端。
如果您希望角度应用程序主动接收来自服务器的消息,则必须在前端和后端使用WebSockets
。有几个教程显示角度服务提供简单的解决方案,遗憾的是不能说C#网站。
否则你可以在一个时间间隔内轮询(HTTP GET)进程的当前状态,这不是完全准确的,但可能是最简单的解决方案。