如何创建接受带有文档附件 属性 的对象的 Web API 服务?
How to create WebAPI service that accepts an object with a document attachment property?
我想创建一个 WebAPI 端点,它接受一个包含 属性 的对象,用于可以附加的文档。此端点应满足 JSON 和 XML 请求,但不确定如何使其正常工作。我环顾四周,但找不到符合我正在寻找的规范的好例子。请在下面查看我需要帮助的解决方案类型:
模型对象:
public class User
{
public User(){
Documents = new List<Document>();
}
public int UserId {get;set;}
public string FirstName {get;set;}
public string LastName {get;set;}
public string Employee {get;set;}
public List<Document> Documents {get;set;} //e.g. Identity Document, Certifications etc
}
public class Document{
public int DocumentId {get;set;}
public string Name {get;set;}
public string Extension {get;set;}
public byte[] Data {get;set;}
}
WebAPI 控制器端点:
[HttpPost]
public IHttpActionResult(User user){
... WHAT TYPE OF CODE DO I NEED HERE ...
}
主要问题是客户端如何也post到这个端点,如果可以的话,你能给我举个例子吗?我的模型对象是否正确?由于字节数组数据不兼容 xml,如何使用 xml 请求从客户端 posted 数据?
这是后端代码(WebApi 控制器)。
[HttpPost]
public IHttpActionResult AcceptUserInfoAndFiles()
{
User userInfo = new User();
var httpContext = HttpContext.Current;
NameValueCollection nvc = HttpContext.Current.Request.Form;
// Fill User data ....
userInfo.UserId=Convert.ToInt32(nvc["UserId"]);
userInfo.FirstName = nvc["FirstName"];
List<Document> documents = new List<Document>();
// Check for any uploaded file
if (httpContext.Request.Files.Count > 0)
{
//Loop through uploaded files
for (int i = 0; i < httpContext.Request.Files.Count; i++)
{
HttpPostedFile httpPostedFile = httpContext.Request.Files[i];
if (httpPostedFile != null)
{
// Get data in byte array
byte[] fileData = null;
using (var binaryReader = new BinaryReader(httpPostedFile.InputStream))
{
fileData = binaryReader.ReadBytes(httpPostedFile.ContentLength);
}
documents.Add(new Document
{
DocumentId = 1, //Generate your document id
Name = httpPostedFile.FileName, // Remove extension if you want to store only name
Extension = System.IO.Path.GetExtension(httpPostedFile.FileName), // Get file extension
Data = fileData
});
}
}
}
userInfo.Documents = documents;
return Ok();
}
这是 jquery 和 html
中的前端示例代码
<div>
<form method="post" action="http://localhost:59462/Api/Values" enctype="multipart/form-data" id="formUpload">
<div>
<label for="files">Files</label>
<input type="file" id="files" name="files" multiple="multiple" />
</div>
<button type="button" id="buttonUpload">Upload files</button>
</form>
</div>
<script>
$(document).ready(function () {
$('#buttonUpload').on('click', function () {
var model = new FormData();
// Get User properties.
model.append('UserId', 1); // Get from UI
model.append('FirstName', "Sam"); // Get from UI
var files = $("#files").get(0).files;
// Add the uploaded file to the form data collection
if (files.length > 0) {
for (f = 0; f < files.length; f++) {
model.append("UploadedImage", files[f]);
}
}
// Ajax upload
$.ajax({
type: "POST",
url: $("#formUpload").attr("action"),
contentType: false,
processData: false,
data: model
});
});
});
</script>
参考链接如下:https://dejanstojanovic.net/aspnet/2018/february/multiple-file-upload-with-aspnet-webapi/
我想创建一个 WebAPI 端点,它接受一个包含 属性 的对象,用于可以附加的文档。此端点应满足 JSON 和 XML 请求,但不确定如何使其正常工作。我环顾四周,但找不到符合我正在寻找的规范的好例子。请在下面查看我需要帮助的解决方案类型:
模型对象:
public class User
{
public User(){
Documents = new List<Document>();
}
public int UserId {get;set;}
public string FirstName {get;set;}
public string LastName {get;set;}
public string Employee {get;set;}
public List<Document> Documents {get;set;} //e.g. Identity Document, Certifications etc
}
public class Document{
public int DocumentId {get;set;}
public string Name {get;set;}
public string Extension {get;set;}
public byte[] Data {get;set;}
}
WebAPI 控制器端点:
[HttpPost]
public IHttpActionResult(User user){
... WHAT TYPE OF CODE DO I NEED HERE ...
}
主要问题是客户端如何也post到这个端点,如果可以的话,你能给我举个例子吗?我的模型对象是否正确?由于字节数组数据不兼容 xml,如何使用 xml 请求从客户端 posted 数据?
这是后端代码(WebApi 控制器)。
[HttpPost]
public IHttpActionResult AcceptUserInfoAndFiles()
{
User userInfo = new User();
var httpContext = HttpContext.Current;
NameValueCollection nvc = HttpContext.Current.Request.Form;
// Fill User data ....
userInfo.UserId=Convert.ToInt32(nvc["UserId"]);
userInfo.FirstName = nvc["FirstName"];
List<Document> documents = new List<Document>();
// Check for any uploaded file
if (httpContext.Request.Files.Count > 0)
{
//Loop through uploaded files
for (int i = 0; i < httpContext.Request.Files.Count; i++)
{
HttpPostedFile httpPostedFile = httpContext.Request.Files[i];
if (httpPostedFile != null)
{
// Get data in byte array
byte[] fileData = null;
using (var binaryReader = new BinaryReader(httpPostedFile.InputStream))
{
fileData = binaryReader.ReadBytes(httpPostedFile.ContentLength);
}
documents.Add(new Document
{
DocumentId = 1, //Generate your document id
Name = httpPostedFile.FileName, // Remove extension if you want to store only name
Extension = System.IO.Path.GetExtension(httpPostedFile.FileName), // Get file extension
Data = fileData
});
}
}
}
userInfo.Documents = documents;
return Ok();
}
这是 jquery 和 html
中的前端示例代码 <div>
<form method="post" action="http://localhost:59462/Api/Values" enctype="multipart/form-data" id="formUpload">
<div>
<label for="files">Files</label>
<input type="file" id="files" name="files" multiple="multiple" />
</div>
<button type="button" id="buttonUpload">Upload files</button>
</form>
</div>
<script>
$(document).ready(function () {
$('#buttonUpload').on('click', function () {
var model = new FormData();
// Get User properties.
model.append('UserId', 1); // Get from UI
model.append('FirstName', "Sam"); // Get from UI
var files = $("#files").get(0).files;
// Add the uploaded file to the form data collection
if (files.length > 0) {
for (f = 0; f < files.length; f++) {
model.append("UploadedImage", files[f]);
}
}
// Ajax upload
$.ajax({
type: "POST",
url: $("#formUpload").attr("action"),
contentType: false,
processData: false,
data: model
});
});
});
</script>
参考链接如下:https://dejanstojanovic.net/aspnet/2018/february/multiple-file-upload-with-aspnet-webapi/