zoukankan      html  css  js  c++  java
  • 百度webuploader上传 1

    百度webupload网址:http://fex.baidu.com/webuploader/

    引入js和css

    <script src="../../Content/webuploader.js"></script>
    <link href="../../Content/webuploader.css" rel="stylesheet" />

    页面html代码

    <div id="uploader" class="wu-example">
    <!--用来存放文件信息-->
    <div id="thelist" class="uploader-list"></div>
    <div class="btns">
    <div id="picker">选择文件</div>
    <button id="ctlBtn" class="btn btn-default">开始上传</button>
    </div>
    </div>

    初始化webupload.js

    <script type="text/javascript">
    // 文件上传
    jQuery(function () {
    var $ = jQuery,
    $list = $('#thelist'),
    $btn = $('#ctlBtn'),
    state = 'pending',
    uploader;

    uploader = WebUploader.create({

    // 不压缩image
    resize: false,

    // swf文件路径
    swf: '/content/Uploader.swf',

    // 文件接收服务端。
    server: '/Webupload/Process',   (这是在后台写的接收前台传送的文件方法)

    // 选择文件的按钮。可选。
    // 内部根据当前运行是创建,可能是input元素,也可能是flash.

    pick: '#picker',
    chunked :true,
    chunkSize: 5242880,(分布上传 一次5mb)

     threads :1(这个是关键  如果不限制同时上传的数目 会导致后台接受的分片错乱  比如按正常的分片第一片应该是开头 但接收的可能就变成第三片从而顺序错乱  这是由于百度webuploader默认允许同时最大上传进程数为3个  所以会导致接受顺序错乱从而重组发生错误)




    });

    // 当有文件添加进来的时候
    uploader.on('fileQueued', function (file) {
    $list.append('<div id="' + file.id + '" class="item">' +
    '<div class="info">' + file.name + '</div>' +
    '<div class="state">等待上传...</div>' +
    '<div class="download" style="display:none;"></div>' +
    '<div class="del"></div>' +
    '</div>');
    });
    uploader.on('uploadProgress', function (file, percentage) {
    var $li = $('#' + file.id),
    $percent = $li.find('.progress .bar');

    // 避免重复创建
    if (!$percent.length) {
    $percent = $('<span class="progress">' +
    '<span class="percentage"><span class="text"></span>' +
    '<span class="bar" role="progressbar" style=" 0%">' +
    '</span></span>' +
    '</span>').appendTo($li).find('.bar');
    }

    $li.find('div.state').text('上传中');
    $li.find("span.text").text(Math.round(percentage * 100) + '%');//显示上传进度
    $percent.css('width', percentage * 100 + '%');
    });

    uploader.on('uploadSuccess', function (file) {
    $('#' + file.id).find('div.state').text('已上传');
    });

    uploader.on('uploadError', function (file) {
    $('#' + file.id).find('div.state').text('上传出错');
    });

    uploader.on('uploadComplete', function (file) {
    $('#' + file.id).find('.progress').fadeOut();
    });

    uploader.on('all', function (type) {
    if (type === 'startUpload') {
    state = 'uploading';
    } else if (type === 'stopUpload') {
    state = 'paused';
    } else if (type === 'uploadFinished') {
    state = 'done';
    }

    if (state === 'uploading') {
    $btn.text('暂停上传');
    } else {
    $btn.text('开始上传');
    }
    });

    $btn.on('click', function () {
    if (state === 'uploading') {
    uploader.stop(true);//不加ture暂停上传没用!
    } else {
    uploader.upload();
    }
    });
    });

    </script>

    后台接收上传的文件的代码(接收接口) 内有上传分布接受

    public class WebuploadController : Controller
    {
    //
    // GET: /Webupload/

    public WebuploadController()
    {
    var applicationPath = VirtualPathUtility.ToAbsolute("~") == "/" ? "" : VirtualPathUtility.ToAbsolute("~");
    urlPath = "/PHP";
    }

    static string urlPath = string.Empty;
    static string guid = Guid.NewGuid().ToString("N");
    static int num = 1;
    public ActionResult Process(string id, string name, string type, string lastModifiedDate, int size, HttpPostedFileBase file)
    {
    string filePathName = string.Empty;

    string localPath = Path.Combine(HttpRuntime.AppDomainAppPath, "PHP");
    if (Request.Files.Count == 0)
    {
    return Json(new { jsonrpc = 2.0, error = new { code = 102, message = "保存失败" }, id = "id" });
    }

    string ex = Path.GetExtension(file.FileName);
    filePathName = guid+"_"+num + ex;//分布接受(建立每个接受分段的名字)
    num++;

    if (!System.IO.Directory.Exists(localPath))
    {
    System.IO.Directory.CreateDirectory(localPath);
    }
    file.SaveAs(Path.Combine(localPath, filePathName));//写入文件夹中

    int total = size / 5242880;//判断有多少个分块
    if(size%5242880>0)
    {
    total += 1;
    }

    //重组

    if (num > total)
    {
    chongzu(total, ex, guid, localPath);
    }

    return Json(new
    {
    jsonrpc = "2.0",
    id = "id",
    filePath = urlPath + "/" + filePathName
    });
    }

    //重组(分片数目,接受视频类型,存储视频的名字,存放的地址)

    public void chongzu(int total, string ex, string guid, string localPath)
    {
    try
    {
    string fileurl = Path.Combine(localPath, guid + ex);

    var fs = new FileStream(fileurl, FileMode.Create);

    for (int i = 1; i <= total; ++i)
    {

    string part = Path.Combine(localPath, guid + "_" + i + ex);

    var bytes = System.IO.File.ReadAllBytes(part);

    fs.Write(bytes, 0, bytes.Length);

    bytes = null;

    System.IO.File.Delete(part);

    }
    fs.Close();

    filenameone = guid + ex;


    }
    catch (Exception e) { Response.Write("<script>alert('视频重组失败') </script>"); }
    finally { num = 1; guid = Guid.NewGuid().ToString("N"); //为了下个视频重置num 和 guid }


    }

    }

    对于上传大的文件需要在配置文件中改iis配置

    <system.web>

    <httpRuntime maxRequestLength="999999999" />//用户上传文件最大体积
    <compilation debug="true" targetFramework="4.0" />

    </system.web>

    <system.webServer>

    <security>

    <requestFiltering>
    <requestLimits maxAllowedContentLength="3000000000" />
    </requestFiltering>
    </security>
    </system.webServer>

    本人菜鸟一个,有写错的或者写的不好地方请各位大神帮忙指正一下,学无止境!
  • 相关阅读:
    Springboot(一)springboot简介与入门程序
    Java基础(二)流程语句与数组
    Java基础(一)基础常识
    Kubernetes介绍
    K8s 存储
    Kubernetes 集群组件
    Kubernetes 设计理念
    Kubernetes 部署与实战
    容器技术与资源隔离
    Docker 网络配置
  • 原文地址:https://www.cnblogs.com/bxbz/p/4186285.html
Copyright © 2011-2022 走看看