Azure存储多个图像上载

时间:2015-04-12 20:34:28

标签: azure windows-phone-8 image-uploading azure-mobile-services

如何从Windows Phone 8将多个图像上传到Azure存储?我找到了上传单张图片的解决方案,但我需要上传3张图片。

我找到的解决方案如下:

http://azure.microsoft.com/documentation/articles/mobile-services-windows-phone-upload-data-blob-storage/

我有一个用户模型,它具有以下属性。

public class User
{
    public long Id { get; set; }

    [JsonProperty(PropertyName = "username")]
    public string UserName { get; set; }

    [JsonProperty(PropertyName = "name")]
    public string Name { get; set; }

    [JsonProperty(PropertyName = "password")]
    public string Password { get; set; }

    [JsonProperty(PropertyName = "phone")]
    public string Phone { get; set; }

    [JsonProperty(PropertyName = "email")]
    public string Email { get; set; }

    [JsonProperty(PropertyName = "gender")]
    public char Gender { get; set; }

    [JsonProperty(PropertyName = "birthdate")]
    public DateTime BirthDate { get; set; }

    [JsonProperty(PropertyName = "location")]
    public string Location { get; set; }

    [JsonProperty(PropertyName = "photo")]
    public System.Windows.Controls.Image Photo { get; set; }


    // For Blob Storage Photo Upload
    [JsonProperty(PropertyName = "containerName")]
    public string ContainerName { get; set; }

    [JsonProperty(PropertyName = "resourceName")]
    public string ResourceName { get; set; }

    [JsonProperty(PropertyName = "sasQueryString")]
    public string SasQueryString { get; set; }

    [JsonProperty(PropertyName = "imageUri")]
    public string ImageUri { get; set; } 

}

而且,为了注册我们的应用程序,用户可以上传个人资料图片。上传个人资料图片的代码如下:

    public RegisterVersion2()
    {
        InitializeComponent();
        SetBinding();
        BuildLocalizedApplicationBar();
        cameraCaptureTask = new PhotoChooserTask();
        cameraCaptureTask.ShowCamera = true;
        cameraCaptureTask.Completed += cameraCaptureTask_Completed;
    }

    void cameraCaptureTask_Completed(object sender, PhotoResult e)
    {
        imageStream = e.ChosenPhoto;
        bmp = new System.Windows.Media.Imaging.BitmapImage();
        bmp.SetSource(e.ChosenPhoto);
        profilPicture.Source = bmp;
    }

    private void photoChoose_Click(object sender, RoutedEventArgs e)
    {
        cameraCaptureTask.Show();

    }
    async void Register(object sender, EventArgs e)
    {
        // Some code for getting input from user for username, email, password etc.I don't put this code here. 

        Model.User newUser = new Model.User
        { Email = email, UserName = username, Password = password, 
        BirthDate = birthday, Name = name + " " + surname, Gender = gender, 
        Phone = phone, ContainerName = username, ResourceName = Guid.NewGuid().ToString() + ".jpg" };
        string errorString = string.Empty;
        DateTime today = DateTime.Today;

        if (imageStream != null)
        {
             await User.InsertAsync(newUser);
        }
        // Send the item to be inserted. When blob properties are set this
        // generates an SAS in the response.


        // If we have a returned SAS, then upload the blob.
        if (!string.IsNullOrEmpty(newUser.SasQueryString))
        {
        // Get the URI generated that contains the SAS 
        // and extract the storage credentials.
              StorageCredentials cred = new StorageCredentials(newUser.SasQueryString);
              var imageUri = new Uri(newUser.ImageUri);
              // Instantiate a Blob store container based on the info in the returned item.
              CloudBlobContainer container = new CloudBlobContainer(
              new Uri(string.Format("https://{0}/{1}",imageUri.Host, newUser.ContainerName)), cred);
              // Upload the new image as a BLOB from the stream.
              CloudBlockBlob blobFromSASCredential = container.GetBlockBlobReference(newUser.ResourceName);
              await blobFromSASCredential.UploadFromStreamAsync(imageStream);

              // When you request an SAS at the container-level instead of the blob-level,
              // you are able to upload multiple streams using the same container credentials.
              imageStream = null;

    }

我的问题是关于上传多张图片。在这个时候,我有一个Item对象,它同样具有与Id,ItemName,ItemCost,ItemDescription等相同的属性。但是,这次我们需要有3个不同的图像上传。以下属性用于一张照片,上传图像的代码也用于一张照片。如何改变呢?

//适用于Blob存储照片上传         [JsonProperty(PropertyName =“containerName”)]         public string ContainerName {get;组; }

    [JsonProperty(PropertyName = "resourceName")]
    public string ResourceName { get; set; }

    [JsonProperty(PropertyName = "sasQueryString")]
    public string SasQueryString { get; set; }

    [JsonProperty(PropertyName = "imageUri")]
    public string ImageUri { get; set; } 

0 个答案:

没有答案