Webuploader 上传指定文件

Webuploader 分片上传多个大文件

 

 <div id="uploader" class="wu-example">
            <div class="btns" style="margin-top: 20px;">
                <div id="picker">选择文件</div>
                <button id="ctlBtn" class="btn btn-default">开始上传</button>
            </div>
            <!--用来存放文件信息-->
            <div id="thelist" class="uploader-list"></div>
        </div>
        <input type="hidden" id="guid" value="@Guid.NewGuid().ToString()" />

 

var uploader = WebUploader.create({
    // swf文件路径
    swf: '/Webuploader/Uploader.swf',

    // 文件接收服务端。
    server: '/Webuploader/fileupload.ashx?guid=' + $("#guid").val() + '',

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

    // 不压缩image, 默认如果是jpeg,文件上传前会压缩一把再上传!
    resize: false,

    //开启分片上传
    chunked: true,

    //上传并发数
    threads: 1,

    accept: {
        title: 'file',
        extensions: 'jpg,mp4,pdf,png,ppt,doc,xls,docx,xlsx,pptx',
        mimeTypes: 'image/jpeg,video/mp4,application/pdf,image/png,application/vnd.ms-powerpoint,application/msword,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.openxmlformats-officedocument.presentationml.presentation'
    }
});

// 当有文件被添加进队列的时候
uploader.on('fileQueued', function (file) {
    $("#thelist").append('<div id="' + file.id + '" class="item">' +
        '<h4 class="info">' + file.name + '</h4>' +
        '<p class="state">等待上传...</p>' +
    '</div>');
});
//上传成功事件
uploader.on('uploadSuccess', function (file) {
    $('#' + file.id).find('p.state').text('上传成功');
});
//上传失败事件
uploader.on('uploadError', function (file) {
    $('#' + file.id).find('p.state').text('上传出错')
});

uploader.on('uploadComplete', function (file) {
    $('#' + file.id).find('.progress').fadeOut();
});
//开始上传事件
$("#ctlBtn").on('click', function () {
    if ($(this).hasClass('disabled')) {
        return false;
    }
    uploader.upload();

});

 

            //如果进行了分片
                    if (context.Request.Form.AllKeys.Any(m => m == "chunk"))
                    {
                        //取得chunk和chunks
                        int chunk = Convert.ToInt32(context.Request.Form["chunk"]);
                        int chunks = Convert.ToInt32(context.Request.Form["chunks"]);
                        //如果不存在就创建file文件夹
                        if (Directory.Exists(context.Server.MapPath("~/Resources/temp/")) == false)
                        {
                            Directory.CreateDirectory(context.Server.MapPath("~/Resources/temp/"));
                        }
                        //根据GUID创建用该GUID命名的临时文件
                        string path = context.Server.MapPath("~/Resources/temp/" + context.Request["guid"]);
                        FileStream addFile = new FileStream(path, FileMode.Append, FileAccess.Write);
                        BinaryWriter AddWriter = new BinaryWriter(addFile);
                        //获得上传的分片数据流
                        file = context.Request.Files[i];
                        Stream stream = file.InputStream;
BinaryReader TempReader
= new BinaryReader(stream); //将上传的分片追加到临时文件末尾 AddWriter.Write(TempReader.ReadBytes((int)stream.Length)); //关闭BinaryReader文件阅读器 TempReader.Close(); stream.Close(); AddWriter.Close(); addFile.Close(); TempReader.Dispose(); stream.Dispose(); AddWriter.Dispose(); addFile.Dispose(); //如果是最后一个分片,则重命名临时文件为上传的文件名 if (chunk == (chunks - 1)) { Url = ResourceConversionURL(context.Request.Files[i].FileName); //文件存在,进行删除 if (File.Exists(context.Server.MapPath(Url))) { File.Delete(context.Server.MapPath(Url)); } FileInfo fileinfo = new FileInfo(path); fileinfo.MoveTo(context.Server.MapPath(Url)); //清空文件夹下所有的临时文件 DirectoryInfo dir = new DirectoryInfo(context.Server.MapPath("~/Resources/temp/")); dir.Delete(true); } } //不是分片上传的 else { file = context.Request.Files[i]; if (file == null || file.ContentLength == 0 || string.IsNullOrEmpty(file.FileName)) continue; filename = Path.GetFileName(file.FileName); Url = ResourceConversionURL(filename); file.SaveAs(HttpContext.Current.Server.MapPath(Url)); } var _result = "{\"jsonrpc\" : \"2.0\", \"result\" : null, \"id\" : \"" + filename + "\"}"; context.Response.Write(_result);

 

posted @ 2017-07-13 11:04  Fly_bokeyuan  阅读(6129)  评论(0编辑  收藏  举报