[英]How can i save an image to a Azure Blob and name it with specific fields and return the Blob Url and save it to database in .net core 2.2
An ios app calls a .net core web API(post method) to send a few details to save in SQL Database. iOS 应用程序调用 .net 核心 Web API(post 方法)发送一些详细信息以保存在 SQL 数据库中。 I have to save this image in a blob and name it as EmpID_PunchInDate_PunchInTime.png
我必须将此图像保存在一个 blob 中并将其命名为 EmpID_PunchInDate_PunchInTime.png
I have the below code but I'm not able to figure out how to call post method inside a post method我有下面的代码,但我无法弄清楚如何在 post 方法中调用 post 方法
/// <summary>
/// The PostPunchInDetailsToAzure
/// </summary>
/// <param name="item">The item<see cref="Punch_In"/></param>
/// <returns>The <see cref="Task{IActionResult}"/></returns>
[HttpPost]
[Route("PostPunchInDetailsToAzure")]
// POST: api/PunchIn/PostPunchInDetailsToAzure
public async Task<IActionResult> PostPunchInDetailsToAzure([FromBody] Punch_In item)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var srtBase64Encoded = item.ClockInNotesForSelfie;
string fullOutputPath = "E:\\temp\\img.png";
byte[] imageBytes = Convert.FromBase64String(srtBase64Encoded);
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
ms.Write(imageBytes, 0, imageBytes.Length);
ms.Position = 0;
System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
System.Drawing.Image img = Base64ToImage(srtBase64Encoded);
img.Save(fullOutputPath, System.Drawing.Imaging.ImageFormat.Png);
var stream = img.ToStream(ImageFormat.Png);
_context.Punch_Ins.Add(item);
await _context.SaveChangesAsync();
return Ok(new { result = "Data stored succesfully." });
}
The incoming request comes in the form of JSON as below (for example)传入请求以 JSON 的形式出现,如下所示(例如)
{
"ClockInEmpId":"1000",
"clockInDate":"10/23/2019",
"ClockInTime":"13:23",
"clockInLatitude":"12.919456",
"clockInLongitude":"77.649802",
"clockInLaborAccountName":"/@H///@H//620108",
"ClockInHQTravelTime":"00:00",
"clockInPerDiem":"00.00",
"clockInNotes":"Hello",
"ClockInNotesForSelfie":"A Base 64 string"
}
So the image should be saved as 1000_10232019_1323.png In Azure Blob and Blob URL should be returned.因此图像应在 Azure Blob 中保存为 1000_10232019_1323.png 并且应返回 Blob URL。 How can i accomplish this?
我怎样才能做到这一点?
First add reference to the azure storage client nuget package.首先添加对 azure 存储客户端 nuget 包的引用。 Then create a new blob container if not exists( as per your requirement) then get a blob reference with the filename.
然后创建一个新的 blob 容器(如果不存在)(根据您的要求),然后获取带有文件名的 blob 引用。
You will need to create the file name from the input request you are getting by picking the fields you need and concatenating them to a string.您需要通过选择您需要的字段并将它们连接到一个字符串来从您获得的输入请求中创建文件名。 Then you create a blob container reference with that filename.
然后使用该文件名创建一个 blob 容器引用。
And then use the Uploadasync method to upload the filestream to that blob.然后使用 Uploadasync 方法将文件流上传到该 blob。 THe blobclient has the uri property which you can return.
blobclient 具有可以返回的 uri 属性。
Also note that if this is a private blob and you are sharing this url to the end user you would have to create the blob URI with a SAS( shared access signature) token appended to the url so that they can download the file directly with that url.另请注意,如果这是一个私有 blob,并且您要将此 url 共享给最终用户,则必须使用附加到 url 的 SAS(共享访问签名)令牌创建 blob URI,以便他们可以直接下载文件网址。 the SAS token will be based on the access policy defined by you to that container.
SAS 令牌将基于您为该容器定义的访问策略。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.