我正在Angular应用程序中使用Kendo Upload控件,并且99%都可以正常工作,但是似乎在我要上传的文本文件的开头添加了一小段文本。
这是HTML定义:
<kendo-upload [saveUrl]="SaveUrl"
(upload)="UploadHandle($event)"
(error)="UploadError($event)"
[restrictions]="Restrictions"
(success)="UploadSuccess($event)"></kendo-upload>
上传处理程序:
UploadHandle(e: UploadEvent): void {
e.data = {
type: 'MasterFile'
};
}
限制和保存网址已被删除
SaveUrl = `${environment.baseUrl}/project/master/file`;
Restrictions: FileRestrictions = {
allowedExtensions: ['.txt']
};
以及接收它的服务器端代码:
public async Task<ActionResult<string>> CreateMaster()
{
if (!Request.HasFormContentType)
{
return BadRequest("No form data received");
}
if (Request.Form.Count != 1)
{
_logger.LogWarning($"Received {Request.Form.Count} forms: {JsonConvert.SerializeObject(Request.Form)}");
return BadRequest(
$"Must pass one and only one form, received {Request.Form.Count}");
}
if (Request.Form.Files.Count != 1)
{
_logger.LogWarning($"Received {Request.Form.Files.Count} Files");
return BadRequest($"Only one file is currently supported, received {Request.Form.Files.Count}");
}
var customerId = this.GetCurrentCustomerId();
var userId = this.GetCurrentUserId();
var sessionId = this.GetCurrentSessionId();
var file = Request.Form.Files[0];
var name = Path.GetFileNameWithoutExtension(file.FileName);
_logger.LogInformation($"Received file with name '{name}': {file.FileName}");
string textData;
using (var sr = new StreamReader(file.OpenReadStream(), Encoding.Unicode))
{
textData = await sr.ReadToEndAsync().ConfigureAwait(false);
}
_logger.LogInformation($"Text data is {textData.Length} characters");
_logger.LogInformation($"Text Data: {textData}");
AwsProject project;
try
{
project = await _service.CreateMasterAsync(
customerId,
name,
new List<string>(),
new List<CollaborateData>(),
textData,
userId,
sessionId).ConfigureAwait(false);
}
catch (InvalidDataException ex)
{
_logger.LogError($"Invalid Data Exception creating master: {ex}");
return BadRequest(
$"Keynote data is not valid, please open this file in Keynote Manager and remove any errors before uploading as a master. {ex.Message}");
}
return CreatedAtAction(nameof(GetProjectById), new { projectId = project.ID }, project);
}
如您所见,我在服务器端进行了大量登录,以试图弄清为什么无法正确解析。刚开始我得到奇怪的结果,因为我没有专门设置编码。我解决了这个问题,这几乎是正确的。唯一不正确的是,它在字符串的开头添加了一些奇怪的字符。在我要上传的文本文件(使用Unicode编码的本地文件)中,第一行如下所示:
A1一些真正有益的笔记
在服务器上的日志中,它看起来像这样:
뿯붿A1一些真正有益的笔记
其他所有内容都是相同的。我还尝试将'true'参数添加到流读取器构造函数中,以使其能够识别BOM(不知道那是不是什么),但没有任何区别。
我想我可以从一开始就将其修剪掉,并且可能会起作用,但是似乎有点过分黑了……有人知道这是从哪里来的,以及如何使它不这样做吗?