0

My code below uploads an Excel file from a user's form submission. However, I'd like to work with the Excel file using EPplus to write it to the database. For that, I need the file's address on the disk, rather than the web address. How can I get that?

Relevant code:

{
    public class ExcelService
    {
        public async Task<string> UploadExcelAsync(HttpPostedFileBase upload)
        {
            string excelFullPath = null;
            if (upload == null || upload.ContentLength == 0)
            {
                return null;
            }
            try
            {
                CloudStorageAccount cloudStorageAccount = ConnectionString.GetConnectionString();
                CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
                CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("excel");

                if (await cloudBlobContainer.CreateIfNotExistsAsync())
                {
                    await cloudBlobContainer.SetPermissionsAsync(
                        new BlobContainerPermissions
                        {
                            PublicAccess = BlobContainerPublicAccessType.Blob
                        }
                        );
                }
                string excelName = Guid.NewGuid().ToString() + "-" + Path.GetExtension(upload.FileName);

                CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(excelName);
                cloudBlockBlob.Properties.ContentType = upload.ContentType;
                await cloudBlockBlob.UploadFromStreamAsync(upload.InputStream);

                excelFullPath = cloudBlockBlob.Uri.ToString();
            }
            catch (Exception ex)
            {

            }
            return excelFullPath;
        }
    }
}
Sartorialist
  • 291
  • 2
  • 18
  • See here: https://stackoverflow.com/questions/8800216/how-to-get-file-physical-location-on-hard-drive – trebleCode Jan 19 '18 at 20:32
  • I don't believe this is relevant to my request. – Sartorialist Jan 19 '18 at 20:42
  • Are you looking for it's memory address or where it physically is on disk? – trebleCode Jan 19 '18 at 20:43
  • I'm not really sure of the distinction. I need the address in the form "/SomeFolder/MyFile.xlsx" and instead all that I'm able to get it a full web URL to the file. – Sartorialist Jan 19 '18 at 20:46
  • If you're uploading to a web address, and you don't own the server you're uploading to or have direct access to IIS or some other webserver, I don't see how you would expect to get where it actually lives on the endpoint you're uploading to – trebleCode Jan 19 '18 at 20:51
  • This surprises me. I'm just trying to read the file name/path into my code here: var fileName = Path.GetFileName(upload.FileName); var path = Path.Combine(Server.MapPath("~/Excel"), fileName); -- This is the code I was using previously when developing locally, but upon migrating to Azure I had to switch to Blob storage method instead of folder. – Sartorialist Jan 19 '18 at 21:12

1 Answers1

1

You can't. Azure Storage Blobs is a cloud service that only exposes web access to the stored blobs, so there is no physical location on a disk you can access. So unless you download the file, edit it and upload it again there is not way to edit it directly.

An alternative could be to use Azure Storage Files. Still an Azure Cloud based service for storage but it allows you to map folders to your machine so you can access it like any network share. See the docs. This, of course, only works if you are able to create a file share on the web server.

Unfortunately you cannot access Azure Storage Files shares using Azure Web Apps (source) so if that it how your web app is hosted than you are out of luck for that.

Peter Bons
  • 26,826
  • 4
  • 50
  • 74