C#版七牛云存储,建立自己的文件库

代码归类 Views
C#版七牛云存储,建立自己的文件库
,排序是个问题官方一直没更新

首先注册七牛云,得到【七牛访问秘钥ACCESS_KEY】、【七牛秘钥密码SECRET_KEY】、【七牛空间Host】、【七牛Bucket(你新建的)】

排序是个问题官方一直没更新,但还是值得一用...
列表读取

using System;
using System.Data;
using HDCMS.Common;
using System.Web.UI.WebControls;
using Qiniu.RSF;
using System.Collections.Generic;
using System.Configuration;
using Qiniu.RS;
using DotNet4.Utilities;
using Qiniu.IO;
using HDCMS.Config;
using System.IO;
using System.Web;
using HDCMS.Core;
namespace HDCMS.Web.Admin.CMSAttachment
{
    public partial class QiniuFiles : HDCMS.Publish.Web.ManagePage
    {
        public string gid = WebUtils.GetQueryString("gId");
        public int mSelectType = WebUtils.GetQueryInt("SelectType", 0);
        public string controlID = WebUtils.GetQueryString("controlID");
        public string typeId = WebUtils.GetQueryString("t") != "" ? WebUtils.GetQueryString("t") : "0";
        public string CategoryID = WebUtils.GetQueryString("CategoryID") != "" ? WebUtils.GetQueryString("CategoryID") : "0";
        public string QiniuDomain = ConfigProvider.Configs.GetString("QiniuDomain", "");//"https://dn-bingnet.qbox.me/";
        public string QiniuBucket = ConfigProvider.Configs.GetString("QiniuBucket", "");//"bing-net";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LinkButton3.PostBackUrl = "ImageList.aspx?gid=" + gid + "&SelectType=" + mSelectType + "&controlID=" + controlID + "&t=1";
            }
            BindData();
        }
        public DateTime Date;
        public int Serial;

        public override string ToString()
        {
            return string.Format("{0} {1}", Date.ToString("yyyy-MM-hh"), Serial);
        }

        static void Sort(List dateList)
        {
            DateTime minDate = new DateTime(0);

            for (int i = 0; i < dateList.Count; i++)
            {
                DateTime tempDate = DateTime.Parse("2099-01-01");

                for (int j = 0; j < dateList.Count; j++)
                {
                    if (dateList[j].Date > minDate)
                    {
                        dateList[j].Serial++;
                        tempDate = dateList[j].Date < tempDate ? dateList[j].Date : tempDate;
                    }
                }
                minDate = tempDate;
            }
        }
        static void DaoSort(List dateList)
        {
            DateTime minDate = new DateTime(0);

            for (int i = 0; i < dateList.Count; i++)
            {
                DateTime tempDate = DateTime.Parse("2099-01-01");

                for (int j = 0; j < dateList.Count; j++)
                {
                    if (dateList[j].Date > minDate)
                    {
                        dateList[j].Serial++;
                        tempDate = dateList[j].Date < tempDate ? dateList[j].Date : tempDate;
                    }
                }
                minDate = tempDate;
            }
        }
        #region  数据绑定送显
        private void BindData()
        {
            Qiniu.Conf.Config.ACCESS_KEY = ConfigProvider.Configs.GetString("Qiniu_AccessKey", "");
            Qiniu.Conf.Config.SECRET_KEY = ConfigProvider.Configs.GetString("Qiniu_SecretKey", "");
            RSFClient rsf = new RSFClient(QiniuBucket);
            rsf.Prefix = "";
            if (txtKeyword.Text.Trim() != "")
            {
                rsf.Prefix = txtKeyword.Text.Trim();
            }
            rsf.Limit = 8;//最多8个

            DataTable tblDatas = new DataTable("Datas");
            tblDatas.Columns.Add("Key", Type.GetType("System.String"));
            tblDatas.Columns.Add("Url", Type.GetType("System.String"));
            tblDatas.Columns.Add("FSize", Type.GetType("System.String"));
            tblDatas.Columns.Add("Date", typeof(DateTime));

            //List dateList = new List();
            //dateList.Add(new DateSerial(DateTime.Parse("2009-03-01")));
            //foreach (DateSerial d in dateList)
            //{
            //    Console.WriteLine(d);
            //}
            System.IFormatProvider format = new System.Globalization.CultureInfo("zh-cn", true);

            List list = new List();
            List items;
            int A = 0;
            if ((items = rsf.Next()) != null)
            {
                while (A < rsf.Limit)
                {
                    var fileDate = items[A].Key.Split('_');//2016-01-12_15-02-32
                    string FileDate = fileDate[0] + " " + fileDate[1].Replace("-", ":");
                    var fileName = items[A].Key;
                    var fSize = items[A].FSize.ToString();
                    //DateTime.ParseExact(FileDate, "yyyy-MM-dd hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture)
                    //dateList.Add(new DateSerial(DateTime.Parse(FileDate), fileName, QiniuDomain + fileName, fSize));
                    tblDatas.Rows.Add(items[A].Key, QiniuDomain + items[A].Key, items[A].FSize, DateTime.Parse(FileDate));
                    A++;
                }
                //items[0].Key;
                //list.AddRange(items);
                //todo
                //dateList.Reverse();                
                //foreach (DateSerial d in dateList)
                //{
                //    Response.Write(d.Key+"
");
                //}
            }
            tblDatas.DefaultView.Sort = "Date desc";
            DataList1.DataSource = tblDatas.DefaultView;
            DataList1.DataBind();
        }
        #endregion
        protected void btnSave_Click(object sender, EventArgs e)
        {

        }
        /// 
  
        /// 得到上传后的文件URL路径。  
        /// 
  
        /// 文件名  
        /// 带有下载口令的URL路径  
        public string MakeGetToken(string key)
        {
            string baseUrl = GetPolicy.MakeBaseUrl(QiniuDomain, key);
            string private_url = GetPolicy.MakeRequest(baseUrl);
            return private_url;
        }
        /// 

        /// 保存文件
        /// 

        /// 
        /// 
        protected void Button1_Click(object sender, EventArgs e)
        {
            UploadFile(FileUpload1.PostedFile);
            BindData();
        }
        /// 
  
        /// 上传文件到网站服务器,再上传到七牛服务器  
        /// 
  
        /// 页面/swf传过来的文件数据  
        /// 预留  
        /// 文件加逻辑路径,如:images/   
        /// 是否保留文件原名  
        /// 不保留原名时,文件名的前缀部分  
        ///   
        public PutRet UploadFile(HttpPostedFile fileData)
        {
            PutRet ret = null;//上传文件需要重新写,必须根据时间格式命名 否则最新8个无法准确获取
            if (fileData != null)
            {
                var fileName = fileData.FileName;
                var fileExt = Path.GetExtension(fileName);
                var serverPath = "/uploadfile/temp/" + DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss") + "_" + fileName;
                bool isImage = false;
                string mimeType = GetMimeTypeByExtension(fileExt);
                if (mimeType.StartsWith("image"))
                {
                    isImage = true;
                }
                if (!System.IO.Directory.Exists(Server.MapPath("/uploadfile/temp/")))
                {
                    System.IO.Directory.CreateDirectory(Server.MapPath("/uploadfile/temp/"));
                }
                fileData.SaveAs(Server.MapPath(serverPath));

                string key = string.Empty;
                //上传图片到七牛  
                if (System.IO.File.Exists(Server.MapPath(serverPath)))
                {
                    // long len = fileData.ContentLength / 1024;  
                    key = serverPath.Replace("/uploadfile/temp/", "").Replace("\\", "/").Replace("//", "/");
                    if (isImage) ret = PutImage(key, Server.MapPath(serverPath), mimeType);
                    else ret = PutFile(key, Server.MapPath(serverPath));
                }
            }
            return ret;
        }
        /// 
  
        /// 普通上传图片  
        /// key必须采用utf8编码,如使用非utf8编码访问七牛云存储将反馈错误  
        /// 
  
        ///   
        /// 上传后的路径  
        /// 要上传的文件路径(文件必须存在)  
        public PutRet PutImage(string key, string fname, string mimeType = "image/jpg")
        {
            if (key.IndexOf('.') == -1) key += Path.GetExtension(fname);
            if (mimeType == "") mimeType = GetMimeTypeByExtension(Path.GetExtension(fname));
            var policy = new PutPolicy(QiniuBucket, 3600);
            string upToken = policy.Token();
            PutExtra extra = new PutExtra
            {
                MimeType = mimeType,
                Crc32 = 123,
                CheckCrc = CheckCrcType.CHECK,
                Params = new Dictionary()
            };
            IOClient client = new IOClient();
            PutRet ret = client.PutFile(upToken, key, fname, extra);
            return ret;
        }
        /// 
  
        /// 普通上传文件  
        /// key必须采用utf8编码,如使用非utf8编码访问七牛云存储将反馈错误  
        /// 
  
        ///   
        /// 上传后的路径  
        /// 要上传的文件路径(文件必须存在)  
        public PutRet PutFile(string key, string fname)
        {
            if (key.IndexOf('.') == -1) key += Path.GetExtension(fname);
            var policy = new PutPolicy(QiniuBucket, 3600);
            string upToken = policy.Token();
            PutExtra extra = new PutExtra();
            IOClient client = new IOClient();
            //client.PutFinished += (o, retTemp) =>  
            //{  
            //    if (retTemp.StatusCode == HttpStatusCode.OK)  
            //    {  
            //    }  
            //    else if (retTemp.StatusCode == HttpStatusCode.BadGateway ||  
            //        retTemp.StatusCode == HttpStatusCode.BadRequest ||  
            //        retTemp.StatusCode == HttpStatusCode.GatewayTimeout ||  
            //        retTemp.StatusCode == HttpStatusCode.GatewayTimeout ||  
            //        retTemp.StatusCode == HttpStatusCode.InternalServerError)
            //    {  
            //    }  
            //};  
            PutRet ret = client.PutFile(upToken, key, fname, extra);

            return ret;
        }
        /// 
  
        /// 通过后缀获取mime类型  
        /// 
  
        ///   
        ///   
        private static string GetMimeTypeByExtension(string extension)
        {
            string mimeType = "";
            extension = extension.TrimStart('.').ToLowerInvariant();
            switch (extension)
            {
                case "gif":
                    mimeType = "image/gif";
                    break;
                case "png":
                    mimeType = "image/png";
                    break;
                case "bmp":
                    mimeType = "image/bmp";
                    break;
                case "jpeg":
                case "jpg":
                    mimeType = "image/jpg";
                    break;

            }
            return mimeType;
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }
        protected void lnkDelete_Click(object sender, EventArgs e)
        {

        }
        protected void btnDeleteMany(object sender, EventArgs e)
        {

        }
    }
}

下 载 人已下载

下载说明:

1、解压密码:无

2、只有部分模板会提供多页面下载,未加说明都是只有一个首页index.html模板。

3、如果您发现文件有错,或者您有其他更好的意见、建议请给我们留言,我们会及时处理!

4、如果您遇到什么问题,也可加入本站QQ1828984798咨询!

*下载本站资源以及作品仅供学习研究之用,若发现任何组织机构及个人有用于商业目的者,必追究其法律责任 *

评论 --
  • 消灭零回复