0

I have a C# web application which uploads an image file to Azure Blob Storage. I am passing the local path of image file from a textbox (no File Upload Controller). This application works locally as expected. But when I publish it on Azure, it throws exception.

Could not find file (filename)

What changes should be made to run it on Azure?

Code :

CloudBlobContainer container = Program.BlobUtilities.GetBlobClient.GetContainerReference(Container);// container 
            container.CreateIfNotExists();
            container.SetPermissions(new BlobContainerPermissions
            {
                PublicAccess = BlobContainerPublicAccessType.Blob
            });
            CloudBlobDirectory directory = container.GetDirectoryReference(foldername);
            // Get reference to blob (binary content)
            CloudBlockBlob blockBlob_image = directory.GetBlockBlobReference(imageid);
                 using (var filestream = System.IO.File.OpenRead(image_path))
                {
                    blockBlob_image.UploadFromStream(filestream);
                }
2
  • 1
    You're trying to 'upload' a file by pointing to the file on your computer. So when you run the website on your computer, it'll work. If you run the website somewhere else, it will not be able to find that file. This is not uploading, it's copying the file. Implement proper uploading to a controller. Commented Jun 7, 2018 at 10:21
  • I am using web forms. not mvc. Also, even using FileUpload Control, it gives same error Commented Jun 7, 2018 at 10:24

3 Answers 3

1

Could not find file (filename)

The exception is caused by System.IO.File.OpenRead(filePath), as you Web Application is published to Azure. If you want use System.IO.File.OpenRead(filePath), you need to make sure that the filepath could be found in the WebApp.

What changes should be made to run it on Azure?

If you want to use the code on the Azure, you need to make sure that the file could be found in the Azue Website. You need to copy the file to Azure. If you want to upload file to the Azure blob, it is not recommanded. Since that you need to copy the file to Azure WebApp first.

Also As you mentioned that you could use FileUpload Controller to do that.

Sign up to request clarification or add additional context in comments.

Comments

0

You're probably using the path to your computer which will be different on azure. You can try change the path to something like this:

string path = HostingEnvironment.ApplicationPhysicalPath + @"\YourProjectName\PathToFile"

3 Comments

I want path like "C:\Users\User\Desktop\1.jpg". 'Using Server.MapPath(FileUpload1.FileName)' or 'Path.GetFullPath(..)' gives different values.
Can you try to edit your post with the code that's failing?
Otherwise it's gonna be hard to identify your problem
0

Ok, Found the solution. Instead of passing file path to a textbox, I used FileUpload Controller. In the code-behind,

Stream image_path = FileUpload1.FileContent;

Actually, earlier too I had tried using FileUpload controller, but Server.MapPath(FileUpload1.Filename) and Path.GetFullPath(FileUpload1.FileName) were not giving the correct path.

Also,

           using (var filestream = image_path)
           {
        blockBlob_image.UploadFromStream(image_path);
           }

is replaced by

blockBlob_image.UploadFromStream(image_path);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.