基本信息
源码名称:C# 上传文件至阿里云OSS实例源码下载
源码大小:1.31M
文件格式:.zip
开发语言:C#
更新时间:2017-03-03
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Aliyun.OpenServices.OpenStorageService;

namespace Oss
{
    /// <summary>
    /// UploadHandler 的摘要说明
    /// </summary>
    public class UploadHandler : IHttpHandler
    {

        OssClient ossClient;
        String bucketName = "hd3p";
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json;charset=UTF-8";
            HttpPostedFile imgFile = context.Request.Files["Filedata"];
            if (imgFile != null)
            {
                string accessid = "xxxxxx";          // AccessID
                string accesskey = "xxxxxxxx";     // AccessKey
                ossClient = new Aliyun.OpenServices.OpenStorageService.OssClient(accessid, accesskey); //当然这里可以封装下
                ObjectMetadata meta = new ObjectMetadata();
                meta.ContentType = "image/jpeg";
                string key = "pic/"   imgFile.FileName;
                PutObjectResult result = ossClient.PutObject(bucketName, key, imgFile.InputStream, meta);//上传图片
                AccessControlList accs = ossClient.GetBucketAcl(bucketName);
                string imgurl = string.Empty;
                if (!accs.Grants.Any())//判断是否有读取权限
                {
                    imgurl = ossClient.GeneratePresignedUri(bucketName, key, DateTime.Now.AddMinutes(5)).AbsoluteUri; //生成一个签名的Uri 有效期5分钟
                }
                else
                {

                    imgurl = string.Format("http://{0}.oss.aliyuncs.com/{1}", bucketName, key);
                } 
                context.Response.Write(String.Format("{0}|{1}|{2}|{3}", imgurl, imgurl, imgurl, key));//
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}