[英]Attach base64 string converted image to request form data in backend?
所以我有一個base64字符串,該字符串將傳遞到一個端點,在該端點中,我需要將其轉換為圖像,然后將其附加到表單數據中。 那可能嗎?
現在我有這樣的事情
Image img = options.base64String.ToImage();
而且我希望將圖像附加到請求的表單數據中,這樣我就可以得到它:
Image img = options.base64String.ToImage();
Request.Form.Files.Add(img); // I want to add the image on something like this
var files = Request.Form.Files;
我想將img附加到Request.Form.Files
另請注意,我只能訪問該應用程序的API。
謝謝!
當然可以。 我有一個解決方案,我也在ASP.NET Web App中使用圖像。
我將圖像以byte[]
存儲在數據庫中,即一個字節數組,並顯示該字節數組中的圖像,但是使用IFormFile
對象對其進行更新。
例如,我有一個存儲在數據庫中的User模型 ,該模型既具有ProfilePic
屬性,又具有另一個模型( ViewModel
)用於更新它。
我的模型到數據庫:
class UserModel
{
public byte[] ProfilePic{get;set;}
}
和我的ViewModel
來顯示或更新用戶的個人資料圖片是這樣的:
public class InputModel
{
// actual image
public byte[] ProfilePic { get; set; }
// image to pass to POST method (so to update)
public IFormFile ProfilePicToUpdate { get; set; }
}
當頁面顯示時,我從數據庫填充我的個人資料照片數組
public async Task<IActionResult> OnGetAsync()
{
var user = await _userManager.GetUserAsync(User);
if (user == null)
{
throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
}
Input = new InputModel
{
ProfilePic = user.ProfilePic,
XP = user.XP,
Address = user.Address,
BirthDate = user.BirthDate
};
return Page();
}
在頁面上顯示個人資料照片,如下所示:
<form method="post" enctype="multipart/form-data">
<h4>Profile picture</h4>
@if (Model.Input.ProfilePic != null)
{
<object class="" data="data:image/png;base64,@Convert.ToBase64String(Model.Input.ProfilePic)" width="224" height="224" type="image/png"></object>
}
else
{
<img class="" src="~/images/profilePicDefault50x50.png" width="224" height="224" />
}
<input asp-for="Input.ProfilePicToUpdate" class="form-control" />
<span asp-validation-for="Input.ProfilePicToUpdate" class="text-danger"></span>
<button type="submit" class="btn btn-default">Save</button>
</form>
然后在Post方法中像這樣處理映像:
public async Task<IActionResult> OnPostAsync(IFormFile file = null)
{
if (!ModelState.IsValid)
{
return Page();
}
if(Input.ProfilePicToUpdate != null && Input.ProfilePicToUpdate.Length > 0)
{
using (var memoryStream = new MemoryStream())
{
await Input.ProfilePicToUpdate.CopyToAsync(memoryStream);
var profilePicBytes = memoryStream.ToArray();
if(user.ProfilePic == null || !profilePicBytes.SequenceEqual(user.ProfilePic))
{
user.ProfilePic = memoryStream.ToArray();
var setProfilePic = await _userManager.UpdateAsync(user);
if (!setProfilePic.Succeeded)
{
throw new ApplicationException($"Unexpected error occurred setting profile picture for user with ID '{user.Id}'.");
}
}
}
}
}
HttpRequest
對象無法修改; 在整個請求期間,它是不變的。 這包括HttpRequest.Form.Files
。
您可以影響請求輸入流數據的處理方式。 由於輸入流包含“表單”字段,因此可能可以使用過濾器來修改請求中的表單數據。 但是,它確實有一些警告:
Form
讀取任何內容),您就來不及了。 Form["Hello"]
可以在這里工作-您需要查看HTTP表單的編碼方式,精確地解析數據以及即時重新創建正確的更改后的表單數據。 除非您真的沒有其他選擇,否則建議您避免這種情況。
一個相對簡單的替代方法是使用實際的請求數據創建一個新的Web請求,並從中返回結果(確保您發出的請求不會引起另一個請求,否則您可能會陷入無限循環,從而阻塞您的服務: ))。 當然,您需要確保所發出的新請求具有所有正確的數據; 沒有簡單的方法可以將HttpRequest
轉換為新的HttpWebRequest
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.