[SDK2.2]Windows Azure Storage (16) 使用WCF服务,将本地图片上传至Azure Storage (上) 客户端代码

简介:

  Windows Azure Platform 系列文章目录

 

  前一章我们完成了服务器端的代码,并且已经发布到了Windows Azure云端。

  本章我们将实现客户端的代码,客户端这里我们使用的是Windows Form。

  源代码下载

 

  1.我们用管理员身份,运行VS2013

  2.新建项目类型为Windows Form Application,并将项目名称修改为LeiAzureClient

  3.本章需要使用HttpClient类来调用WCF,需要进行相关的配置:

  点击Project LeiAzureClient,右键,选择Manage NuGet Packages。如下图

  

  4.在弹出的窗口里,查询"Microsoft HTTP Client Libraries",查询完毕后,点击Install

  5.安装完毕后,我们回到项目文件的Form1,在窗口里增加一个按钮,并设置Text为UploadPic

  

  6.在Form1.cs的引用内容如下:

复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Net.Http;
using System.Net.Http.Headers;
using System.IO;
复制代码

  7.增加如下代码:

复制代码
     string urlPrefix = "http://leiazureservice.cloudapp.net/service1.svc";
        private void button1_Click(object sender, EventArgs e)
        {
            string uriString = urlPrefix + @"/UploadPic";

            using (HttpClient client = new HttpClient())
            {
                //Please copy file to C:\
                var fs = new FileStream(@"c:\\WP7.jpg", FileMode.Open, FileAccess.Read);

                var ms = new MemoryStream();
                fs.CopyTo(ms);

                ByteArrayContent arrayContent = new ByteArrayContent(ms.ToArray());
                arrayContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");

                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));

                var response = client.PostAsync(new Uri(uriString, UriKind.Absolute), arrayContent).Result;

                //lblResult.Text = resp.ToString();
                if (response.IsSuccessStatusCode)
                {
                    //will return Uploaded GUID
                    string picName = response.Content.ReadAsStringAsync().Result.ToString();

                    //Please check the Upload Photos URL AT
                    //http://leiwcfstorage.blob.core.windows.net/photos/{GUID}.jpg
                    //e.g. http://leiwcfstorage.blob.core.windows.net/photos/39c51e48-e758-8697-d402-8df45f071d40.jpg
                }

            }
        }
复制代码

  在上面的代码中,我们实现以下功能:

  1)指定Azure WCF URL

  2)将本地已经存在的C:\WP7.jpg文件上传至Azure Storage

  3)如果服务器端的response.IsSuccessStatusCode返回True,则调用WCF成功

  4)response.Content.ReadAsStringAsync().Result.ToString();将会返回服务器端的结果

  

  8.我们执行Windows Form工程,点击UploadPic按钮,查看response.IsSuccessStatusCode返回结果

  如果返回结果为Ture,我们打开IE浏览器,查看到leiwcfstorage里新建了名为photosContainer

  

  9.点击上图photos,我们可以查看到上传成功的图片。如下图:

  

分类:  Azure Storage

本文转自Lei Zhang的博客博客园博客,原文链接:http://www.cnblogs.com/threestone/p/3401591.html,如需转载请自行联系原作者
目录
相关文章
|
8月前
|
NoSQL IDE MongoDB
Studio 3T 2025.11 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
Studio 3T 2025.11 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
482 3
|
7月前
|
安全 生物认证 网络安全
windows10无法设置默认保存位置怎么办?显示错误代码0x80070002怎么解决?
Win10系统下载文件时,默认会保存在特定位置,但用户可自行修改。若更改后仍无效,可通过删除目标磁盘中的特定文件夹、修改注册表权限、“干净启动”排除干扰软件或使用第三方修复工具等方式解决此问题。
1437 0
|
4月前
|
Linux 虚拟化 iOS开发
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
994 0
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
|
5月前
|
NoSQL IDE MongoDB
Studio 3T 2025.17 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
Studio 3T 2025.17 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
378 1
Studio 3T 2025.17 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
|
11月前
|
SQL 数据库 Windows
YashanDB Windows客户端安装
本文介绍YashanDB客户端(Windows)的安装、使用及卸载步骤。首先,下载并解压软件包至本地路径,配置环境变量。接着,通过cmd窗口使用yasql命令连接数据库,执行SQL操作。最后,卸载时删除相关环境变量和客户端目录。更多功能请参考官方文档。
YashanDB Windows客户端安装
|
8月前
|
NoSQL IDE MongoDB
Studio 3T 2025.10 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
Studio 3T 2025.10 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
760 21
Studio 3T 2025.10 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
|
6月前
|
Windows
office出现0xc0000142错误?windows错误代码为0xc0000142?
office出现0xc0000142错误?windows错误代码为0xc0000142?
301 0
|
7月前
|
Linux 虚拟化 iOS开发
VMware Remote Console 13.0.0 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
VMware Remote Console 13.0.0 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
1776 0
VMware Remote Console 13.0.0 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
|
9月前
|
API 开发工具 网络架构
【Azure Service Bus】使用Python SDK创建Service Bus Namespace资源(中国区)
本文介绍了如何使用Python SDK创建Azure Service Bus Namespace资源。首先,通过Microsoft Entra ID注册应用获取Client ID、Client Secret和Tenant ID,完成中国区Azure认证。接着,初始化ServiceBusManagementClient对象,并调用`begin_create_or_update`方法创建资源。
242 29
|
10月前
|
SQL 数据库 Windows
YashanDB Windows客户端安装
本文详细介绍了YashanDB客户端(Windows)的安装、使用与卸载步骤。安装部分包括解压软件包至本地路径,并配置系统环境变量;使用部分通过cmd窗口运行`yasql`命令连接远程数据库,执行相关操作;卸载部分则说明了删除环境变量及客户端目录的具体方法。更多功能可参考官方文档中的C驱动、imp、exp、yasldr和yasql工具说明。
YashanDB Windows客户端安装