0

I have this project deployed on azure website (running on one instance) that gets different fields (name, age, ...) and also allows an upload of an image. All data is then stored in an azure database.

The problem is that I can't save the uploaded file. It works fine locally but when deployed I get the "Could not find a part of the path 'D:...." error.

I have tried saving the file in Temp and localresources but I must be missing something or implementing incorrectly, I also don't have access to the blob Storage.

How can I properly save the uploaded file (which I would like to store in a db)?

This is how and where I'm trying to save the image:

[HttpPost]
    public ActionResult Create(HttpPostedFileBase file, [Bind(Include = "StudentId,Name,Age")] student13 student13)
    {
        if (file != null && file.ContentLength > 0)
        {
            var fileName = Path.GetFileName(file.FileName);
            var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
            file.SaveAs(path);
        }

This is the view:

<form action="" method="post" enctype="multipart/form-data">
   <label for="file">picture:</label>
    <input type="file" name="file" id="file" class="form-control" />
    <input type="submit" class="btn btn-default" value="Create" />
</form>
mkrieger1
  • 19,194
  • 5
  • 54
  • 65

1 Answers1

0

It is possible to write on the file system of Azure Websites, however your write permissions are limited to the root folder of your app. you shall be able to write anywhere within the Server.MapPath("~/from_here_on"), check out this for details.

You could refer to How to Use Azure Blob Storage with Azure Web Sites to connect your website with azure storage also save images to storage. And here is the latest documentation about Azure StorageGet started with Azure Blob storage using .NET. Hope it do help.

[HttpPost]
public ActionResult ImageUpload()
{
    var image = Request.Files["image"];
    if (image == null)
    {
        ViewBag.UploadMessage = "Failed to upload image";
    }
    else
    {
        ViewBag.UploadMessage = String.Format("Got image {0} of type {1} and size {2}",
        image.FileName, image.ContentType, image.ContentLength);
        // TODO: actually save the image to Azure blob storage
    }
    return View();
}
Community
  • 1
  • 1
Steven
  • 804
  • 4
  • 12