2016-11-01 2 views
1

Ich versuche, Dateien in Blob Azure aus einem MVC-App zu laden, ich folge this Tutorial, und ich bin in diesem Snippet stecken:Datei hochladen in Blob Azure

using (var fileStream = System.IO.File.OpenRead(@"path\myfile")) 
{ 
    blockBlob.UploadFromStream(fileStream); 
} 

Wie @"path\myfile" machen dynamische ?? Wie kann ich den tatsächlichen Pfad meiner Datei abrufen, um sie dort einzufügen? Ich nehme auch anderen Vorschlag blob hochladen :)

UPDATE

Wie schlug ich den Code in meiner Ansicht hinzugefügt:

@model RiPSShared.Models.RiPSModels.AgencyNote 

<h2>PostFile</h2> 

<form action="@Url.Action("PostFile", "AgencyNotes", new { NoteId=Model.aut_id})" method = "post" enctype="multipart/form-data"> 
    <label for="file1"> File name:</label> 
    <input type="file" name="file" id="file1" /> 
    <input type="submit" value="Submit" />  
</form> 

und die vollen Action Controller:

public ActionResult PostFile(HttpPostedFileBase file, int NoteId) 
{ 
CloudBlockBlob blockBlob = container.GetBlockBlobReference(path); 
using (Stream fileStream = file.InputStream) 
      { 
       blockBlob.UploadFromStream(fileStream); 
      } 
return RedirectToAction("Index"); 
} 

Antwort

5

HttpPostedFileBase werden die Informationen haben, die Sie benötigen. Insbesondere die InputStream Eigenschaft geben Sie den Stream

[HttpPost] 
public ActionResult PostFile(HttpPostedFileBase file, int NoteId) 
{ 
    // Your existing code for azure blob access 
    CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob"); 
    // make sure to a null check on file parameter before accessing the InputStream 

    using (var s = file.InputStream) 
    { 
     blockBlob.UploadFromStream(s); 
    } 
    // to do :Return something 
} 
+1

nur darauf warten, 1 Minute erhalten es als Antwort zu wählen funktioniert super !!! – AlexGH

+0

Ich schlage vor, dass Sie die relevanten Informationen (es gibt Formularpost aus Ihrer Sicht und Ihre Aktionsmethode param ist HttpPostedFileBase-Typ) in die Frage einbeziehen, um zukünftigen Lesern zu helfen. Ich wusste es, weil ich deine vorherige Frage gelesen habe. – Shyju

+1

danke! Es ist jetzt aktualisiert :) – AlexGH

1

Auch setzen Sie Ihre Verbindungszeichenfolge in der Konfigurationsdatei und erhalten Sie von diesem und Speicherdatei in Blob Speicher zunächst seine Containername

 //StorageConnectionString string specified in configuration file 
     CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
     CloudConfigurationManager.GetSetting("StorageConnectionString")); 

     // Create the blob client. 
     CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); 

     // Retrieve a reference to a container. 
     //specified container name 
     CloudBlobContainer container = blobClient.GetContainerReference("myBlobcontainer"); 

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

     container.SetPermissions(
     new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob }); 

     CloudBlockBlob blockBlob = container.GetBlockBlobReference("myBlob"); 

     // Create or overwrite the "myblob" blob with contents from a local file. 
     using (var fileStream = file.InputStream) 
     { 
      blockBlob.UploadFromStream(fileStream); 
     } 
Verwandte Themen