In my web application, I need to allow the user to choose an image from their machine and upload it, and have that image physically stored to my container in my storage in Azure.
The way to do this I have found in many places and it works for me, but with the IMPORTANT EXCEPTION that in all examples, they upload a file from their own machine and knowing or harcoding the physical path.
But as for security reasons, the input file cannot read the physical address of the client, I was forced to use the strategy of:
The process cannot access the file. 'C:\My_folders\MyFile.jpg' because it is being used by another process.
Note: I tried using a project directory and also the temporary directory of the operating system. For both cases I get the same result.
So, in conclusion: I need to upload to Azure S Blob, but as far as I could find out, for that I need the physical path of the file, so I need to upload it to my server and from there upload it.
I would like you to help me with these options:
Thank you very much! _________________________ CODE ________________________ FRONT
<form method="POST" enctype="multipart/form-data" id="fileUploadForm">
<div class="row">
<div class="col-md-12" id="conten_CargaImagenes">
<div class="card card-success">
<div class="card-header">
<h3 class="card-title" id="tituloAbmHorarios">
<i class="far fa-image"></i> Nueva Imagen
</h3>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<input asp-for="File" class="form-control custom-file-input" />
<label id="fileImageLabel" asp-for="File" class="custom-file-label "></label>
</div>
</div>
<div class="col-md-3">
<button type="button" class="btn bg-gradient-success btn-sm pull-left" onclick="GuardarImagen()">
<i class="fas fa-file-upload"></i> Grabar Imagen
</button>
</div>
</div>
</div>
</div>
</div>
</div>
JS Method
function GuardarImagen() {
var form = $('#fileUploadForm')[0];
var model = new FormData(form);
model.append('IdComercio', $('#Id').val());
$.ajax({
type: "POST",
enctype: 'multipart/form-data',
url: "/Comercios/GrabaFile",
data: model,
processData: false,
contentType: false,
cache: false,
timeout: 600000,
success: function (e) {
if (!e.isError) {
$("#File").val(null);
document.getElementById("fileImageLabel").innerHTML = '';
toastr.success(textos.imagenGrabadaOk, { timeOut: 2000 });
InjectarNuevaImagenEnPantalla(e.data);
} else {
toastr.error(e.data, { timeOut: 2000 });
}
}
});
}
BACK
[HttpPost]
public ReturnData GrabaFile(UploadFileComercioDTO pData)
{
if (pData.File != null)
{
try
{
var result = UploaderFilesService.UploadToAzure(pData.File, FolderPath, pData.IdComercio.ToString());
}
catch (Exception ex)
{
do something...
}
}
else
{
return messageError
}
}
public static ReturnData UploadToAzure(IFormFile pFile, string pFolder, string pIdComercio)
{
ReturnData returnData = new();
if (pFile != null)
{
if (ValidaFile(pFile.ContentType, pFile.Length))
{
try
{
string nombreOriginal = pFile.FileName;
var nombrePartes = nombreOriginal.Split(".");
string extension = nombrePartes[(nombrePartes.Length - 1)];
string nombreFinal = GenerarTextoUnico() + "." + extension;
string soloNOmbre = nombreFinal; // ****
pFolder = Path.GetTempPath(); // *************
string filePath = Path.Combine(pFolder, nombreFinal);
using (var stream = File.Create(filePath))
{
pFile.CopyTo(stream);
}
string connectionString = "adsfasfasdfasdf";
string containerName = "asdfasdf";
nombreFinal = pIdComercio + "/" + nombreFinal;
BlobContainerClient container = new BlobContainerClient(connectionString, containerName);
var upload = container.UploadBlob(nombreFinal, File.OpenRead(filePath));
try
{
File.Delete(filePath);
}
catch (IOException e)
{
Console.WriteLine(e.Message);
}
}
catch (Exception ex)
{
...
}
}
else
{
...
}
}
else
{
...
}
return returnData;
}
I found the problem and the solution.
The problem was that it was not closing the stream when uploading.
The modification to make was in the UploadToAzure method, change the following line
var upload = container.UploadBlob(finalName, File.OpenRead(filePath));
for this
using (var stream = File.OpenRead(filePath))
{
var upload = container.UploadBlob(endName, stream);
}
That way when I exit using the stream is already closed and I can proceed to delete it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.