Is there a way to create a byte array from a file in JavaScript (or Typescript) that will be sent to a C# WebApi service, and consumable by the C# byte array method parameter? If so, can you provide an example of the JavaScript?
I should mention, this is an Angular 4+ application that will be posting to the service.
I need to upload Word documents, PDFs and images to a C# service, and this is the way the C# service is currently built. It expects a byte array of the file being uploaded.
This is the request object of the C# service:
[DataContract]
public class SaveAttachmentRequest : BaseRequest
{
[RestUrlParameter(Mode = UrlParameterMode.Inline)]
[DataMember(IsRequired = true)] public AttachmentLookupInformation LookupInformation { get; set; } = new AttachmentLookupInformation();
[DataMember(IsRequired = true)] public byte[] Attachment { get; set; } = null;
}
And this is the AttachmentLookupInformation class:
[DataContract]
public class AttachmentLookupInformation
{
[DataMember(IsRequired = true)] public Guid Id { get; set; } = Guid.Empty;
[DataMember(IsRequired = true)] public Guid LinkedToId { get; set; } = Guid.Empty;
///<summary>Not including the path- just the file name and extension.</summary>
[DataMember(IsRequired = true)] public string FileName { get; set; } = string.Empty;
[DataMember(IsRequired = true)] public int FileSizeInBytes { get; set; } = 0;
///<summary>MIME type.</summary>
[DataMember(IsRequired = true)] public string ContentType { get; set; } = string.Empty;
[DataMember(IsRequired = true)] public AttachmentCategories AttachmentCategory { get; set; } = AttachmentCategories.Unknown;
[DataMember(IsRequired = true)] public string DownloadUrl { get; set; } = string.Empty;
[DataMember(IsRequired = true)] public string CreatedBy { get; set; } = string.Empty;
[DataMember(IsRequired = true)] public DateTimeOffset CreatedTimestamp { get; set; } = DateTimeOffset.MinValue;
}
This is so I can upload the file and additional data to the service.
EDIT: including failed Angular code that I used:
const lookupInfo = new AttachmentLookupInformation();
lookupInfo.Id = Helpers.emptyGuid;
lookupInfo.LinkedToId = this.contractId;
lookupInfo.AttachmentCategory = 10;
const request = new SaveAttachmentRequest();
const reader = new FileReader();
reader.onload = function() {
const buffer = reader.result;
let binary = '';
const bytes = new Uint8Array(buffer);
const length = bytes.byteLength;
for (let i = 0; i < length; i++) {
binary += String.fromCharCode(bytes[i]);
}
request.Attachment = binary;
lookupInfo.ContentType = files[0].type;
lookupInfo.FileName = files[0].name;
lookupInfo.FileSizeInBytes = length;
};
reader.readAsArrayBuffer(files[0]);
reader.onloadend = function() {
request.LookupInformation = lookupInfo;
console.log('request: ', request);
that.contractsService
.save(request, 'attachment/saveattachment')
.subscribe(res => {
if (res.Success) {
console.log('upload response: ', res);
}
});
};