C# 上传本地视频到七牛云服务器

第一步添加DLL引用

Install-Package Newtonsoft.Json

Install-Package Qiniu

   #region 七牛云视频上传
        /// <summary>
        /// 七牛云视频上传
        /// </summary>
        /// <param name="fileData"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UploadQiniuVideo(HttpPostedFileBase fileData)
        {
            if (fileData != null)
            {
                try
                {
                    string SaveName = DateTime.Now.ToString("yyyyMMddHHmmssffff");
                    Mac mac = new Mac("******************", "**********************");
                    string bucket = "csgk";
                    BinaryReader b = new BinaryReader(fileData.InputStream);
                    byte[] data = b.ReadBytes((int)fileData.InputStream.Length);
                    PutPolicy putPolicy = new PutPolicy();
                    putPolicy.Scope = bucket;
                    putPolicy.SetExpires(3600);
                    string jstr = putPolicy.ToJsonString();
                    string lic = Auth.CreateUploadToken(mac, jstr);
                    UploadManager um = new UploadManager();
                    um.UploadData(data, SaveName, lic);
                    return Json(new { Success = true, FilePath = "http://***********.com/" + SaveName, SaveName = SaveName });                
                }
                catch (Exception ex)
                {
                    return Json(new { Success = false, Message = ex.Message }, JsonRequestBehavior.AllowGet);
                }
            }
            else
            {
                return Json(new { Success = false, Message = "请选择要上传的文件!" }, JsonRequestBehavior.AllowGet);
            }
        }
        #endregion

若通过路径读取文件: 

byte[] data = System.IO.File.ReadAllBytes(s); 

其中s为文件的路径

posted @ 2019-06-06 14:44  代码沉思者  阅读(846)  评论(1编辑  收藏  举报