Hello Synfusion Team,
How can I upload an image to our own database via custom API and based upon the response, I want to show failed or success message.
onUpload() should be called when upload button is clicked as shown below.
getImageFromAzure(request) is our API call that gives a json response in below format.
{
"success": true,
"statusCode": 201,
"message": "Created successfully.",
"data": 'imageURL'
}
Steps:
Thank You
Ansuman
|
<ejs- uploader #defaultupload id='defaultfileupload' [autoUpload]='false'[asyncSettings]='path' [dropArea]='dropElement' >
</ejs-uploader>
|
|
public path: Object = {
saveUrl: 'http://localhost:51370/api/SampleData/Save',
removeUrl: 'http://localhost:51370/api/SampleData/Remove'
};
|
|
[HttpPost("[action]")]
public async Task Save(IList<IFormFile> UploadFiles)
{
try
{
foreach (var file in UploadFiles)
{
const string accountName = "***"; // Provide your accountName
const string key = "***"; // Provide your account key
var storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, key), true);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("filo"); // Provide your container name
await container.CreateIfNotExistsAsync();
await container.SetPermissionsAsync(new BlobContainerPermissions()
{
PublicAccess = BlobContainerPublicAccessType.Blob
});
var blob = container.GetBlockBlobReference(file.FileName);
using (var stream = file.OpenReadStream())
{
await blob.UploadFromStreamAsync(stream);
}
}
}
catch (Exception e)
{
Response.Clear();
Response.StatusCode = 400;
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "File failed to upload";
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = e.Message;
Response.Headers.Add("ID", "Maximum Uploaded files reached");
}
}
} |