简体   繁体   English

C# - 如何将文件上传到 Azure 存储 Blob(未知物理路径)

[英]C# - How to upload files to Azure Storage Blob (unknown phisic path)

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.在我的 web 应用程序中,我需要允许用户从他们的机器中选择一个图像并上传它,并将该图像物理存储到我存储在 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:但是出于安全考虑,输入文件无法读取客户端的物理地址,我只好采用了以下策略:

  1. I upload the image to the web server to a known path.我将图像上传到 web 服务器的已知路径。
  2. I take that path, and I use it to upload to Azure Storage Blob.我走那条路,用它上传到 Azure Storage Blob。
  3. Confirmed this, I don't want that file to stay on the server , I just want it to be in my container , so I proceed to delete the file.确认了这一点,我不希望该文件保留在服务器上,我只希望它在我的容器中,所以我继续删除该文件。
  4. I get the following exception:我得到以下异常:

The process cannot access the file.该进程无法访问该文件。 'C:\My_folders\MyFile.jpg' because it is being used by another process. 'C:\My_folders\MyFile.jpg' 因为它正被另一个进程使用。

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.所以,总而言之:我需要上传到 Azure S Blob,但据我所知,为此我需要文件的物理路径,所以我需要将它上传到我的服务器并从那里上传。

I would like you to help me with these options:我希望你能帮助我做出这些选择:

  1. To be able to upload it without the physical path.为了能够在没有物理路径的情况下上传它。 Or to be able to close the process I have now, ensuring the deletion of the temporary file on the server.或者能够关闭我现在拥有的进程,确保删除服务器上的临时文件。
  2. Any of your suggestions.您的任何建议。

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 JS方法

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.问题是上传时没有关闭 stream。

The modification to make was in the UploadToAzure method, change the following line要进行的修改是在 UploadToAzure 方法中,更改以下行

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.这样当我使用stream 退出时已经关闭,我可以继续删除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Blazor 将大文件上传到 WebAPI (C#) 并将其存储到 Azure Blob 存储中 - How to upload large file using Blazor to WebAPI (C#) and store it into Azure Blob storage Azure 使用标签的 blob 存储搜索 c# - Azure blob storage search using tags c# Azure function C#:写入 Azure 上的块 blob(csv 文件)存储帐户创建了两个版本的 blob - Azure function C#: Writing to Block blob (csv file) on Azure Storage Account creates two versions of the blob 如何在 blob 存储中上传具有原始文件夹结构的文件? - how to upload files with its original folder structure in blob storage? Azure 逻辑应用程序如何将多个 JSON 文件合并到 Blob 存储 - Azure Logic Apps how to combine multiple JSON files to Blob storage 创建并上传 PDF 到 azure blob 存储 - Create and upload PDF to azure blob storage 直接从浏览器上传文件到 Azure Blob 存储? - Upload file to Azure Blob Storage directly from browser? 将文件从 Azure blob 存储移动到 Google 云存储桶 - Moving Files from Azure blob storage to Google cloud storage bucket 为什么json文件上传到azure blob存储c#时出现Rare characters? - Why does Rare characters appears when json file is uploaded to azure blob storage c#? 如何从 azure blob 存储中读取“.owl” - How to read an ".owl" from azure blob storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM