使用xamarin.forms将图像上传到windows azure

时间:2017-04-01 14:13:31

标签: xamarin.forms

我正在尝试创建一个简单的xamarin.forms应用程序,用于从电话库中选择图像并将i上传到azure存储服务。 这就是我所做的,但不幸的是我收到了错误消息 " {Microsoft.WindowsAzure.Storage.StorageException:服务器无法验证请求。"

厨房代码中的拣货图片:

 private MediaFile mediaFile;
    public ImageUpload ()
    {
        InitializeComponent ();
    }


    private async void Button_Clicked(object sender, EventArgs e)
    {
        await CrossMedia.Current.Initialize();
        if(CrossMedia.Current.IsPickPhotoSupported==false)
        {
            await DisplayAlert("No Pick", "No Available", "ok");
            return;
        }

        this.mediaFile = await CrossMedia.Current.PickPhotoAsync();
        if (this.mediaFile == null)
            return;

        //Show The path in the Xaml
        LocalImagPath.Text = this.mediaFile.Path;
        //Show the image in the Xaml
        MyImageFile.Source = ImageSource.FromStream(this.mediaFile.GetStream);



    }//end btnClikcked

    private async void uploadPhoto_Clicked(object sender, EventArgs e)
    {
        try
        {
            var imageName = await ImageManager.UploadImage(this.mediaFile);
        }
        catch(Exception ex)
        {
            ex.StackTrace.ToString();
        }


    }//end uploadPhoto_Clicked

这是ImageManger类: 类ImageManager     {         private static Random random = new Random();

    // Uploads a new image to a blob container.
    public static async Task<string> UploadImage(MediaFile image)
    {
        CloudBlobContainer container = GetContainer();

        // Creates the container if it does not exist
        //await container.CreateIfNotExistsAsync();

        // Uses a random name for the new images
        var name = Guid.NewGuid().ToString();


        // Uploads the image the blob storage
        CloudBlockBlob imageBlob = container.GetBlockBlobReference(name);
        imageBlob.Properties.ContentType = "image/jpeg";

        await imageBlob.UploadFromFileAsync(image.Path);


        return name;
    }




    // Gets a reference to the container for storing the images
    private static CloudBlobContainer GetContainer()
    {
        // Parses the connection string for the WindowS Azure Storage Account
        CloudStorageAccount account = CloudStorageAccount.Parse(Configuration.StorageConnectionString);
        CloudBlobClient client = account.CreateCloudBlobClient();

        // Gets a reference to the images container
        CloudBlobContainer container = client.GetContainerReference("bepartphoto");

        //Set The Permissions
        BlobContainerPermissions blopPerm = new BlobContainerPermissions();
        blopPerm.PublicAccess = BlobContainerPublicAccessType.Container;
        container.SetPermissionsAsync(blopPerm);

        return container;
    }//end GetContainer


}//end class

0 个答案:

没有答案