问题
I'm using the Kendo Upload control in an Angular app and it's 99% working, but it seems to be adding a small piece of text to the start of the text file I'm uploading.
Here is the HTML definition:
<kendo-upload [saveUrl]="SaveUrl"
(upload)="UploadHandle($event)"
(error)="UploadError($event)"
[restrictions]="Restrictions"
(success)="UploadSuccess($event)"></kendo-upload>
The upload handler:
UploadHandle(e: UploadEvent): void {
e.data = {
type: 'MasterFile'
};
}
The restrictions and save URL refrenced:
SaveUrl = `${environment.baseUrl}/project/master/file`;
Restrictions: FileRestrictions = {
allowedExtensions: ['.txt']
};
And the server side code receiving it:
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);
}
As you can see I put a lot of logging into the server side to try to figure out why it wasn't parsing properly. At first I was getting strange results because I wasn't setting the encoding specifically; I fixed that and it's almost correct. The only thing that it's not getting right is that it's adding a few strange characters to the start of the string. In the text file I'm uploading (local file using Unicode encoding) the first line looks like this:
A1 Some Note That Is Really Informative And Helpful
In the log on the server it looks like this:
뿯붿A1 Some Note That Is Really Informative And Helpful
Everything else is identical. I have also tried to add the 'true' argument to the stream reader constructor to make it recognize BOM (didn't know if that's what it was) but it didn't make any difference.
I guess I can just trim that off the start and it would probably work, but that seems a bit overly hackish... Does anyone know where that is coming from and how to get it not to do that?
来源:https://stackoverflow.com/questions/56758642/kendo-angular-upload-adds-to-start-of-text-file