gpt4 book ai didi

javascript - 在 JavaScript 中创建字节数组

转载 作者:行者123 更新时间:2023-11-28 03:45:23 26 4
gpt4 key购买 nike

有没有办法从 JavaScript(或 Typescript)中的文件创建字节数组,该数组将发送到 C# WebApi 服务,并可由 C# 字节数组方法参数使用?如果是的话,您能提供一个 JavaScript 示例吗?

我应该提到,这是一个 Angular 4+ 应用程序,将发布到该服务。

我需要将Word文档、PDF和图像上传到C#服务,这就是C#服务目前的构建方式。它需要一个正在上传的文件的字节数组。

这是C#服务的请求对象:

[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;
}

这是 AttachmentLookupInformation 类:

[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;
}

这样我就可以将文件和其他数据上传到服务。

编辑:包括我使用的失败的 Angular 代码:

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);
}
});

};

最佳答案

那么,你有:

that.contractsService
.save(request, 'attachment/saveattachment')

但是您显示的类是 SaveAttachmentRequest,其中包含 public byte[] Attachment。您的问题中没有显示名为 attachment 的类中名为 saveattachment 的函数,该函数应该处理将数据传输到 byte[] 的操作,我想,除非你以某种方式将其直接设置到参数中。假设您有这样一个 attachment 类,那么您应该在其中有一个名为 saveattachment 的入口点来完成所有工作。但可以说它失败了,因为您正在传递 JS 模型并将其直接映射到 .NET 模型。然后创建一个可以接受二进制字符串的字符串参数,然后将其保存到您的 byte[] 参数中。

[DataMember(IsRequired = true)] public string strAttachment { get; set; } = null;

在你的 JS 中,

request.Attachment = binary;

变成了

request.strAttachment = binary;

那么你需要:

public class attachment : ApiController
{
[System.Web.Http.HttpPost]
public JsonResult saveattachment(SaveAttachmentRequest sar, AttachmentLookupInformation ali)
{
//string bits = "000011110000001000";
string bits = sar.strAttachment;
int numOfBytes = (int)Math.Ceiling(bits.Length / 8m);
byte[] bytes = new byte[numOfBytes];
int chunkSize = 8;

for (int i = 1; i <= numOfBytes; i++)
{
int startIndex = bits.Length - 8 * i;
if (startIndex < 0)
{
chunkSize = 8 + startIndex;
startIndex = 0;
}
bytes[numOfBytes - i] = Convert.ToByte(bits.Substring(startIndex, chunkSize), 2);
}

sar.attachment = bytes;

// ... do your other stuff for saving your model objects to the DB
// using Entity Framework's data context, etc.
}
}

这只是将二进制字符串转换为 byte[] 的一种方法。还有很多其他方式here .

或者,您是否尝试过将其作为字符串发送,从而实现:

[DataMember(IsRequired = true)] public byte[] Attachment { get; set; } = null;

变成这样:

[DataMember(IsRequired = true)] public string Attachment { get; set; } = null;

首先?

关于javascript - 在 JavaScript 中创建字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48528017/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com