C#上传demo

原创 2015年07月10日 14:06:10

1,这是用winform写的

using Qiniu.IO;
using Qiniu.IO.Resumable;
using Qiniu.RPC;
using Qiniu.RS;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class UP : Form
    {
        //public event EventHandler<PutNotifyEvent> Notify1;
        //public event EventHandler<PutNotifyErrorEvent> NotifyErr1;
        public UP()
        {
            InitializeComponent();
        }
//上传方法的调用事件。
        private void button2_Click(object sender, EventArgs e)
        {
            //Qiniu.Conf.Config.UP_HOST = "http;//up.qiniu.com";
           // PutFile("liuhanlin-work", "yinji", "F:\\迅雷下载\\郭敬明 陈砺志 李力《乱入小时代》_高清.mp4");
            //ResumablePutFile("liuhanlin-work", "gif", "A:\\buty-picture\\gif");
            ResumablePutFile("flytest", "4838_848ca76a004611e584f8d73f7f367d12.f0.mp4", "A:\\buty-picture\\4838_848ca76a004611e584f8d73f7f367d12.f0.mp4");
        }
        //这是直传的函数
        public static void PutFile(string bucket, string key, string fname)
        {
            var policy = new PutPolicy(bucket, 3600);
            System.Console.WriteLine(policy);
            string upToken = policy.Token();
            PutExtra extra = new PutExtra();
            IOClient client = new IOClient();
           PutRet ret= client.PutFile(upToken, key, fname, extra);
           ret.Response.ToString();
            //client.PutFileWithoutKey
        }
//如下是断点上传的函数。
        public  void ResumablePutFile(string bucket, string key, string fname)
        {
            Console.WriteLine("\n===> ResumablePutFile {0}:{1} fname:{2}", bucket, key, fname);
            PutPolicy policy = new PutPolicy(bucket, 3600);
            policy.PersistentOps = "avthumb/mp4/ab/512k/s/640x360;avthumb/mp4/ab/1024k/s/1280x720";
            policy.PersistentNotifyUrl = "http://baidu.com/index.html";
            string upToken = policy.Token();
            textBox1.Text = upToken;
            Settings setting = new Settings();
            ResumablePutExtra extra = new ResumablePutExtra();
            extra.Notify += new EventHandler<PutNotifyEvent>(extra_Notify);
            extra.NotifyErr += new EventHandler<PutNotifyErrorEvent>(extra_NotifyErr);
            ResumablePut client = new ResumablePut(setting, extra);
           // CallRet ret = client.PutFile(upToken, fname, Guid.NewGuid().ToString());
            CallRet ret = client.PutFile(upToken, fname, key);
            if (ret.OK)
            {
                MessageBox.Show("ok");
                MessageBox.Show(ret.Response);


            }
            else
            {
                MessageBox.Show(ret.Response);
            }


        }

        private static void extra_Notify(object sender, PutNotifyEvent e)
        {
            e.BlkIdx.ToString();
            e.BlkSize.ToString();
            Console.WriteLine(e.BlkIdx.ToString()+ e.BlkSize.ToString());
          //  e.Ret.offset.ToString();
            //2015年三月23日,提交了


        }

        private static void extra_NotifyErr(object sender, PutNotifyErrorEvent e)
        {
            e.BlkIdx.ToString();
            e.BlkSize.ToString();

        }

        private void UP_Load(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }


    }
}
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:C#上传demo
举报原因:
原因补充:

(最多只允许输入30个字)