简体   繁体   中英

interacting with blob storage on MS Azure from ASP.NET C#

I am working on a website, I should deploy this on Azure cloud after I finish development, the thing is that my web app should upload a blob from the user local machine to the blob storage my question is , how do I get the file path of the file? I use upload control to enable the user to upload a file when the user clicks the submit button there is code executed to upload the file specified in the upload control to the blob storage how can I get the value of the filepath variable in my code? does the file need to be uploaded on the server that is hosting my web app, or I can just use the path of the file from the client's local machine? this is the upload web form code:

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.Azure;

namespace myProject
{
    public partial class popout : System.Web.UI.Page
    {


        protected void Button1_Click(object sender, EventArgs e)
        {
            string filePath;
            if (FileUpload1.PostedFile != null)
            {
                filePath = FileUpload1.PostedFile.FileName;
            }
            else { filePath = null; }

            // file name with path of the file uploaded using FileUpload1 control
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("connectionstringWhatever"));
                // Create a blob client.
                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

                // Get a reference to a container named “myContainer”
                CloudBlobContainer container = blobClient.GetContainerReference("myContainer");
                //setting the permission to public
                container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });


                // Retrieve reference to a blob named  "blob1".or if doesn't exist, create one
                CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob1");
            if (filePath != null)
            {
                using (var fileStream = System.IO.File.OpenRead(filePath))
                {

                    blockBlob.UploadFromStream(fileStream);
                }
            }
            else {
                string msg = "FileUpload1 = null";
                ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + msg + "');", true);

            }
        }
        }
    }

Take a look at this page on MSDN, particularly the example:

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.postedfile(v=vs.110).aspx

This is a similar scenario to yours. The example saves the uploaded file to the server, then performs additional operations on it. However, you may also be able to write the file data to the blob so long as the file data is in memory, using the PostedFile property.

You won't be able to provide the file path to the file on the user's machine. The upload path is provided through the upload dialog only; you don't have access to that path from your application.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM