Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
470 views
in Technique[技术] by (71.8m points)

asp.net mvc - Upload Picture to Windows Azure Web Site

I have an ASP.NET MVC 4 app that i want to deploy to Windows Azure. A part of this app involves uploading a picture. When the picture is uploaded, I want to store the picture in a directory located at /pictures/uploaded.

My question is, how do I upload a picture to a relative path within my app hosted on Windows Azure? Up to this point, my app has been hosted in a Virtual Machine. I was able to do the above by using the following:

string path = ConfigurationManager.AppSettings["rootWebDirectory"] + "/pictures/uploaded;

// Get the file path
if (Directory.Exists(path) == false)
  Directory.CreateDirectory(path);

string filePath = path + "/uploaded" + DateTime.UtcNow.Milliseconds + ".png";
filePath = filePath.Replace("/", "").Replace("", "");

// Write the picture to the file system
byte[] bytes = GetPictureBytes();
using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
{
  fileStream.Write(bytes, 0, bytes.Length);
  fileStream.Flush();
  fileStream.Close();
}

Currently, ConfigurationManager.AppSettings["rootWebDirectory"] points to an absolute path. I belive this is where my problem lies. I can't figure out how to switch all of this to a relative path.

Thank you!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is my as easy as it comes description of how to set this up in azure. http://geekswithblogs.net/MagnusKarlsson/archive/2012/12/02/how-to-use-azure-storage-for-images.aspx

//Edit; heres the complete example from my blog(if the blog dies). yourViewName.cshtml

 @model List<string>  
 @{
     ViewBag.Title = "Index";
 }

 <h2>Index</h2>
 <form action="@Url.Action("Upload")" method="post" enctype="multipart/form-data">

     <label for="file">Filename:</label>
     <input type="file" name="file" id="file1" />
     <br />
     <label for="file">Filename:</label>
     <input type="file" name="file" id="file2" />
     <br />
     <label for="file">Filename:</label>
     <input type="file" name="file" id="file3" />
     <br />
     <label for="file">Filename:</label>
     <input type="file" name="file" id="file4" />
     <br />
     <input type="submit" value="Submit" />

 </form>

 @foreach (var item in Model) {

     <img src="@item" alt="Alternate text"/>
  }

Your controller action

public ActionResult Upload(IEnumerable<HttpPostedFileBase> file)
         {
             BlobHandler bh = new BlobHandler("containername");
             bh.Upload(file);
             var blobUris=bh.GetBlobs();

             return RedirectToAction("Index",blobUris);
         }

Your model

 public class BlobHandler
     {
         // Retrieve storage account from connection string.
         CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
         CloudConfigurationManager.GetSetting("StorageConnectionString"));

         private string imageDirecoryUrl; 

         /// <summary>
         /// Receives the users Id for where the pictures are and creates 
         /// a blob storage with that name if it does not exist.
         /// </summary>
         /// <param name="imageDirecoryUrl"></param>
         public BlobHandler(string imageDirecoryUrl)
         {
             this.imageDirecoryUrl = imageDirecoryUrl;
             // Create the blob client.
             CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

             // Retrieve a reference to a container. 
             CloudBlobContainer container = blobClient.GetContainerReference(imageDirecoryUrl);

             // Create the container if it doesn't already exist.
             container.CreateIfNotExists();

             //Make available to everyone
             container.SetPermissions(
                 new BlobContainerPermissions
                 {
                     PublicAccess = BlobContainerPublicAccessType.Blob
                 });
         }

         public void Upload(IEnumerable<HttpPostedFileBase> file)
         {
             // Create the blob client.
             CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

             // Retrieve a reference to a container. 
             CloudBlobContainer container = blobClient.GetContainerReference(imageDirecoryUrl);

             if (file != null)
             {
                 foreach (var f in file)
                 {
                     if (f != null)
                     {
                         CloudBlockBlob blockBlob = container.GetBlockBlobReference(f.FileName);
                         blockBlob.UploadFromStream(f.InputStream);
                     }
                 }
             }
         }

         public List<string> GetBlobs()
         {
             // Create the blob client. 
             CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

             // Retrieve reference to a previously created container.
             CloudBlobContainer container = blobClient.GetContainerReference(imageDirecoryUrl);

             List<string> blobs = new List<string>();

             // Loop over blobs within the container and output the URI to each of them
             foreach (var blobItem in container.ListBlobs())
                 blobs.Add(blobItem.Uri.ToString());

             return blobs;
         }
     }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...