将字节数组发送到 API

Sending byte array to API

我正在将 Web 应用程序转换为移动应用程序。我们在 Web 应用程序中使用 AjaxAsyncFileUpload 将文档保存到服务器,其中 AjaxAsyncFileUpload 用于为我完成工作。这是代码

Dim fileData As Byte() =new Byte(AjaxAsyncFileUpload.FileContent.Length-1){}
AjaxAsyncFileUpload.FileContent.Read(fileData, 0, fileData.Length)

InvestmentDeclare.DocSize  = AjaxAsyncFileUpload.FileContent.Length
InvestmentDeclare.DocFileName = AjaxAsyncFileUpload.FileName
InvestmentDeclare.DocFileType = AjaxAsyncFileUpload.PostedFile.ContentType
InvestmentDeclare.Document = fileData

然后将其保存到我的数据库中。

现在,在将其转换为移动应用程序时(我也在为移动应用程序使用 c#),我无法传递字节数组。我正在使用 fiddler 进行测试。 我附上了 image 我如何通过 fiddler 传递它。在我的 API POST 方法中,我为我的文档变量获取了一个空值,同时我能够正确获取其余值。

可能是什么问题?我没有以正确的 Json 格式传递字节吗?

在API中:

public class AddInvestmentDeclare
{
 public int EmployeeId { get; set; }
 public int YearId { get; set; }
 public int InvestmentId { get; set; }
 public List<EmpDocument> EmpDocuments { get; set; }
}

public class EmpDocument
{
 public byte[] Document { get; set; }
 public string DocumentFileName { get; set; }
 public long DocumentSize { get; set; }
 public string DocumentType { get; set; }
}

public HttpResponseMessage Post(int YearId, [FromBody]List<AddInvestmentDeclare> InvestmentDeclared)
{

当我在 运行 时间检查我的 InvestDeclared 列表时,我发现文档变量未填充并且显示为空。我也附上了 image。

首先,设置您的 API 方法,使其只接受一个包含所有数据的对象,而不是单独接受每个参数。

而不是:

public HttpResponseMessage Post([FromUri]string AccordDbName, [FromUri]String PayCareDbName, [FromUri]...)

设置如下:

public HttpResponseMessage Post(Data dataObject)

其次,尝试将文件作为 base64 字符串发送。 Here 是如何操作的示例。

您在 JSON 请求中的文件应如下所示:

{ "ID":46, 
  "Content":"JVBERi0xLjQNCjEgMCBvYm......"
}