跟我学Windows Azure 五 使用Cloub Service连接Blob Service完成图片的上传
首先,我们创建一个云服务项目,用来演示我们的blob存储

下来我们修改我们我们云服务的名字


我们需要添加一个空的WebForm的项目

点击完成,我们可以看到我们的解决方案已经添加完成

下来我们需要添加一个实体类,用来存储文件的信息.


下面是我们实体类的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace BlobWebRole
{
public class ImageEn
{
public string BlobName { get; set; }
public Uri FileUri { get; set; }
public string FileName { get; set; }
public long Length { get; set; }
public string FolderName { get; set; }
public bool IsReadOnly { get; set; }
public bool IsExists { get; set; }
public string FullFileName { get; set; }
public string ExtendName { get; set; }
public string CreatorName { get; set; }
public DateTime CreateTime { get; set; }
}
}
下来我们需要添加一个页面窗体,用来进行图片的显示和上传


下面是Default.aspx前台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="BlobWebRole.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div><!--上传图片-->
<asp:Label ID="lblFilePath" Text="上传文件" AssociatedControlID="fileUploadControl"
runat="server" />
<asp:FileUpload ID="fileUploadControl" runat="server" />
<asp:RequiredFieldValidator ID="filUploadValidator" ControlToValidate="fileUploadControl"
ValidationGroup="fileInfoGroup" ErrorMessage="请选择文件!" runat="Server">
</asp:RequiredFieldValidator>
<br />
<asp:Label ID="lblFileName" Text="文件名称:" AssociatedControlID="txtFileName" runat="server" />
<asp:TextBox ID="txtFileName" runat="server" />
<asp:RequiredFieldValidator ID="fileNameValidator" ControlToValidate="txtFileName"
ValidationGroup="fileInfoGroup" ErrorMessage="请输入文件名!" runat="Server">
</asp:RequiredFieldValidator>
<br />
<asp:Label ID="lblCreator" Text="创 建 者:" AssociatedControlID="txtCreator" runat="server" />
<asp:TextBox ID="txtCreator" runat="server" />
<asp:RequiredFieldValidator ID="submitterValidator" ControlToValidate="txtCreator"
ValidationGroup="fileInfoGroup" ErrorMessage="请输入创建者!" runat="Server">
</asp:RequiredFieldValidator>
<br />
<asp:Button ID="btnSave" Text="保存" CausesValidation="true" ValidationGroup="fileInfoGroup"
runat="server" OnClick="btnSave_Click" />
<br />
<br />
<asp:Label ID="statusMessage" runat="server" />
</div>
<div><!--列表显示图片-->
<asp:GridView ID="fileView" AutoGenerateColumns="false" DataKeyNames="BlobName" runat="server"
OnRowCommand="RowCommandHandler">
<Columns>
<asp:ButtonField Text="Delete" CommandName="DeleteItem" />
<asp:HyperLinkField HeaderText="Link" DataTextField="FileName" DataNavigateUrlFields="FileUri" />
<asp:BoundField DataField="CreatorName" HeaderText="Created by" />
<asp:BoundField DataField="FolderName" HeaderText="FolderName" />
<asp:BoundField DataField="IsReadOnly" HeaderText="IsReadOnly" />
<asp:BoundField DataField="IsExists" HeaderText="IsExists" />
<asp:BoundField DataField="FullFileName" HeaderText="FullFileName" />
<asp:BoundField DataField="ExtendName" HeaderText="ExtendName" />
<asp:BoundField DataField="CreateTime" HeaderText="CreateTime" />
</Columns>
</asp:GridView>
</div>
</div>
</form>
</body>
</html>
下来添加我们Default.aspx.cs后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System.IO;
using System.Collections.Specialized;
namespace BlobWebRole
{
public partial class Default : System.Web.UI.Page
{
private CloudBlobClient _BlobClient = null;
private CloudBlobContainer _BlobContainer = null;
private const string conContainerAddress = "imagefiles";
private const string conConfigStorageSetting = "BlobConnectionString";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.EnsureContainerExists();
DisplayFileList();
}
}
private void EnsureContainerExists()
{
var container = GetContainer();
// 检查container是否被创建,如果没有,创建container
container.CreateIfNotExists();
var permissions = container.GetPermissions();
//对Storage的访问权限是可以浏览Container
permissions.PublicAccess = BlobContainerPublicAccessType.Container;
container.SetPermissions(permissions);
}
private CloudBlobContainer GetContainer()
{
//获取ServiceConfiguration.cscfg配置文件的信息
var account = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("BlobConnectionString"));
var client = account.CreateCloudBlobClient();
//获得BlobContainer对象
return client.GetContainerReference(conContainerAddress);
}
protected void btnSave_Click(object sender, EventArgs e)
{
//获取扩展名
string extension = System.IO.Path.GetExtension(fileUploadControl.FileName);
//获得BlobContainer对象并把文件上传到这个Container
var blob = this.GetContainer().GetBlockBlobReference(Guid.NewGuid().ToString() + extension);
blob.UploadFromStream(fileUploadControl.FileContent);
////设置元数据到Blob
blob.Metadata["FileName"] = txtFileName.Text;
blob.Metadata["Length"] = fileUploadControl.FileContent.Length.ToString();
DirectoryInfo di = new DirectoryInfo(string.Format("{0}\\", conContainerAddress));
blob.Metadata["FolderName"] = di.Name;
FileInfo file = new FileInfo(fileUploadControl.PostedFile.FileName);
blob.Metadata["IsReadOnly"] = file.IsReadOnly.ToString();
blob.Metadata["IsExists"] = file.Exists.ToString();
blob.Metadata["FullFileName"] = file.FullName;
blob.Metadata["ExtendName"] = file.Extension;//extension
blob.Metadata["CreatorName"] = txtCreator.Text;
blob.Metadata["CreateTime"] = DateTime.Now.ToShortTimeString();
blob.SetMetadata();
//设置属性
blob.Properties.ContentType = fileUploadControl.PostedFile.ContentType;
blob.SetProperties();
//显示列表
DisplayFileList();
}
/// <summary>
/// 列表
/// </summary>
private void DisplayFileList()
{
var blobs = this.GetContainer().ListBlobs(null, false, BlobListingDetails.Metadata, null, null).OfType<CloudBlockBlob>();
var filesList = new List<ImageEn>();
//查询
foreach (var blobItem in blobs)
{
//实体字段
filesList.Add(new ImageEn()
{
FileUri = blobItem.Uri,
BlobName=blobItem.Name,
FileName = txtFileName.Text,
Length = long.Parse(blobItem.Metadata["Length"]),
FolderName = blobItem.Metadata["FileName"],
IsReadOnly = bool.Parse(blobItem.Metadata["IsReadOnly"]),
IsExists = bool.Parse(blobItem.Metadata["IsExists"]),
FullFileName = blobItem.Metadata["FullFileName"],
ExtendName = blobItem.Metadata["ExtendName"],
CreatorName = blobItem.Metadata["CreatorName"],
CreateTime = DateTime.Parse(blobItem.Metadata["CreateTime"])
});
}
//绑定列表
fileView.DataSource = filesList;
fileView.DataBind();
}
protected void RowCommandHandler(object sender, GridViewCommandEventArgs e)
{
//删除
if (e.CommandName == "DeleteItem")
{
// 获取索引
var index = Convert.ToInt32(e.CommandArgument);
var blobName = fileView.DataKeys[index].Value.ToString();
//Get the container
var blob = this.GetContainer().GetBlockBlobReference(blobName);
blob.Delete();
}
//更新列表
DisplayFileList();
}
}
}
我们登陆WindowsAzure管理门户去添加一个存储.

我们添加名为teacherblob的存储服务

下来我们双击我们Studio2013中的云服务,点击设置,去设置他所要连接的设置

我们把连接字符串的名称叫做BlobConnectionstring,然后选择类型为连接字符串,然后值那里我们选择我的订约,然后会自动带出,我们刚才在Windows azure上建立的blob,选择他就可以了.


然后我们选择我们的BlobStorageDemo进行调试

我们在页面中输入需要上传得图片及对应的属性.点击保存按钮,保存按钮成功后,列表中会显示我们的图片文件信息.

下来我们可以在我们studio中看到我们存储的文件

我们同时也可以使用azure storage explorer看到我们我们上传得文件

打完收工!
跟我学Windows Azure 五 使用Cloub Service连接Blob Service完成图片的上传的更多相关文章
- [New Portal]Windows Azure Virtual Machine (15) 在本地制作数据文件VHD并上传至Azure(2)
<Windows Azure Platform 系列文章目录> 在上一章内容里,我们已经将包含有OFFICE2013 ISO安装文件的VHD上传至Azure Blob Storage中了. ...
- [New Portal]Windows Azure Virtual Machine (14) 在本地制作数据文件VHD并上传至Azure(1)
<Windows Azure Platform 系列文章目录> 之前的内容里,我介绍了如何将本地的Server 2012中文版 VHD上传至Windows Azure,并创建基于该Serv ...
- 跟我学Windows Azure 四 Cloud Service中的WebRole与WorkRole,及他们之间的通信
Cloud Service 中WebRole就相当与我们的WebSite,而WorkRole相当与我们在服务器上写了个Windows Service,站在高可用的角度上来讲,Cloud Service ...
- 重新想象 Windows 8.1 Store Apps (89) - 通信的新特性: 下载数据, 上传数据, 上传文件
[源码下载] 重新想象 Windows 8.1 Store Apps (89) - 通信的新特性: 下载数据, 上传数据, 上传文件 作者:webabcd 介绍重新想象 Windows 8.1 Sto ...
- PHP代码篇(五)--如何将图片文件上传到另外一台服务上
说,我有一个需求,就是一个临时功能.由于工作开发问题,我们有一个B项目,需要有一个商品添加的功能,涉及到添加商品内容,比如商品名字,商品描述,商品库存,商品图片等.后台商品添加的接口已经写完了,但是问 ...
- 【Azure 应用服务】App Service 在使用GIt本地部署,上传代码的路径为/home/site/repository,而不是站点的根目录/home/site/wwwroot。 这个是因为什么?
问题描述 App Service 在使用GIt本地部署,上传代码的路径为/home/site/repository,而不是站点的根目录/home/site/wwwroot. 这个是因为什么? 并且通过 ...
- 跟我学Windows Azure 三 使用vs2013创建windows azure web site
首先我们需要登陆我们的windows azure上,然后访问 https://manage.windowsazure.cn/publishsettings/index 他会让我们下载我们的订阅证书文件 ...
- 跟我学Windows Azure 二 使用SQL Azure创建数据库服务器,创建数据库,创建表
登陆Windows Azure门户 输入我们上一节课所注册的帐号及密码,点击登陆. 选择SQL 数据库,选择服务器 选择创建数据库服务器 设置访问数据库服务器的登陆帐号及密码 点击确定完成数据库服务器 ...
- 跟我学Windows Azure 一 创建Windows Azure试用账号
我在网上看了很多教程,很大部分都是申请的是国外或者是香港的试用账号,而国内是由世纪互联所代理的,他的申请方式与VS2013的部署设置或多或少还是有些出入,这里我先跟大家一起过一下,在国内如何申请一个w ...
随机推荐
- 对ToString("X2 ")的理解
/// <summary> /// 将byte型转换为字符串 /// </summary> /// <param name=&q ...
- TeamWork-天气美食
一. 团队情况 Hello,欢迎来到我们"Code Man"队的第一次团队作业页面,"代码侠"很明显我们是一个编程队伍,由大三在读的6位同班同学组成 ...
- Monte Carlo方法简介(转载)
Monte Carlo方法简介(转载) 今天向大家介绍一下我现在主要做的这个东东. Monte Carlo方法又称为随机抽样技巧或统计实验方法,属于计算数学的一个分支,它是在上世纪四十年代 ...
- PKU1008
题名:玛雅历 题意:历法转换 . 代码: // 1008.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iost ...
- MySQL语句中的转义字符----引号
MySQL语言中的转义字符和各种编程语言基本相同,见下表 形式 含义 \0 0(NUL)字符 \n 换行 \r 回车符 \t 制表符 \b 退格 \' 单引号 \" 双引号 \\ 反斜线 \ ...
- 使用FlaycoBanner实现图片轮播效果(加载网络图片)
FlaycoBanner是一个开源图片轮播框架,支持android2.2及以上: git地址:https://github.com/H07000223/FlycoBanner_Master 在andr ...
- CURL常用命令
下载单个文件,默认将输出打印到标准输出中(STDOUT)中 curl http://www.centos.org 通过-o/-O选项保存下载的文件到指定的文件中:-o:将文件保存为命令行中指定的文件名 ...
- win7系统下的FTP配置
2016-07-12 工作中需要在win7操作系统下配置FTP,遇到许多问题,所以记录下来方便以后解决问题. FTP是文件传输协议的简称.用于Internet上的控制文件的双向传输.同时,它也是一个应 ...
- Xcode6中如何去掉默认的Main.storyboard
xcode 6取消了 Empty Application 模板来创建一个工程,创建出来的有工程多了Main.storyboard,默认加载Main.storyboard,但是有很多人还想用代码来实现U ...
- SQL --Chater03 聚合与排序
数据说明: +-----------+------------+---------------+--------------+--------------+------------+ | shohin ...