js ftp upload


     <div class="container"> 
       <div class="row">
           <div class="col-md-6">
                 <input type="text"  id="txtfiledesc"   class="layui-input">
             </div> 
            <div class="col-md-6">
                <div class="layui-upload">
        
  <input type="button" class="layui-btn" id="op1uplload"  value="Upload File"/>
  
</div> 
             </div> 
           </div>

     </div>
      
     <div class="container"> 
       <div class="row">
           <div class="col-md-1">
             </div> 
        <div class="col-md-5">
            <div id="div1" runat="server"  class="popup-gallery"   >
                   
            <table id="tbl1"   class="dts" style="width:100%;border:1px solid #0094ff;border-collapse:collapse;  "    >
            </table> 
               </div>
        </div>
        <div class="col-md-5">
             
               
            <table id="tbl2"   class="dts"     style="width:100%;border:1px solid #0094ff;border-collapse:collapse;  " >

            </table>
           
        </div>
         <div class="col-md-1">
             </div> 
    </div> 
      </div>
                    


    <script>
        $(document).ajaxStart(function () { SetBlockUI(90000); }).ajaxStop(function () { ClearBlockUI(); });
        var tbl1, tbl2;
        var tbdt1 = new Array();
        var tbdt2 = new Array();
        function getuploaddata() {
            console.log("getuploaddata");
            var json = {
                "ASPSESSID": "hh", "ActionName": "MultiFile", "folder": "QMS", "subfolder": "", "sysid": "WorkFlow", "form_id": "xxx",
                "path": "ftp://www.qmtec.net/kaFastFlow/Flow/18081121524585016", "waterWords": "N",
                "form_type": "888888",
                "location": "Item",
                "operation": "op",
                "level": 8,
                "memo": $('#txtfiledesc').val(),
                "lot": "lot",
                "item_id": "item",
                "item_desc": "name",
                "user_id": "user",
                "user_name": "username",
                "vendor": "vendor",
                "status": "New",
                "trx_id": "trxid",
                "info": "info",
                "authority": "user"
            }
            return json;
        }
       
        layui.use('upload', function () {
            var $ = layui.jquery
                , upload = layui.upload;

            //普通图片上传
            var uploadInst = upload.render({
                elem: '#op1uplload'
                , method: "post"
               
                , url: 'http://localhost/WorkFlow/httpserver/formftp.ashx'
                , multiple: false
                , accept: 'file' //普通文件
                , before: function (obj) {
                    //预读本地文件示例,不支持ie8
                    this.data = getuploaddata();
                    layer.load(); //上传loading
                }
                , done: function (res) {
                    layer.closeAll('loading'); //关闭loading
                    if (res.code == "000") {
                        //do something (比如将res返回的图片链接保存到表单的隐藏域)


                        layer.msg('上传成功');
                        
                    }
                    else {

                        layer.msg("上传失败  " + res.msg);
                    }

                    Loadtbl1();
                }
                , error: function () {
                    layer.closeAll('loading'); //关闭loading
                    layer.msg('上传失败');
                }
            });

        });
    </script>

    <script>

        var ftpcolumns = [
            { data: "memo", title: "memo", },
         
            { data: "refid", title: "refid", visible: false, },
             
            {
                data: "file_name", title: "File", className: "bk",

                render: function (data, type, row, meta) {
                    if (row.file_type == 'Pic') {
                        return '<a   style="color:#fff;" target="_blank"  href="' + row.http_path + '">' + '<img src="' + row.http_path  + '" width="20" height="20" /> ' + '</a > ';

                    }
                    else {
                        return '<a   style="color:#fff;" target="_blank"  href="' + row.http_path +   '">' + row.file_name + '</a> ';
                        
                    }

                }
            },
            {
                data: null, title: "Del", sClass: "hidCol",
                render: function (data, type, row, meta) {
                    
                    return " <input type='button' class=\"layui-btn layui-btn-danger layui-btn-xs\" name='btndel' lay-event=\"del\"  value='Delete' />";
                }

            }


        ];

        function TableDel(index, code, msg) {

            tbl1.ajax.reload();
             
        }

        function ShowHidDel() {
            if ($('#hidaction').val() != "Create") {
                $("input[name='btndel']").css("display", "none");
                $(".hidCol").css("display", "none");
            }

            if (tbdt1.length == 0) {
                $('#tbl1').css('display', 'none');
            }
            else {
                $('#tbl1').css('display', 'table');
            }

            if (tbdt2.length == 0) {
                $('#tbl2').css('display', 'none');
            }
            else {
                $('#tbl2').css('display', 'table');
            }
        }

        function Loadtbl1() {

            var ftpjsondata = {
                "cmd": "QueryByFormLocation",
                "location": "Item",
                "form_id": "xxx",
                "level": 999999999
            };

            if (tbl1 != undefined) {
                tbl1.destroy();
            }

            tbl1 = $('#tbl1').DataTable({
                ajax: {
                    type: "post",
                    url: "http://localhost/workflow/HttpServer/ftpserver.ashx",
                    dataType: "json",
                    crossDomain: true,
                    retrieve: true,
                    timeout: 60000,
                    data: function (d) {
                        //  d.action = "W1";
                        return JSON.stringify(ftpjsondata)
                    },

                    dataSrc: function (json) {
                        tbdt1 = [];
                        tbdt2 = [];
                        if (json) {

                            if (json.data != null || json.data != undefined) {

                                json.data.forEach(function (value, index, array) {
                                    if (value.file_type != "Pic") {
                                        console.log(index);
                                        tbdt2.push(value);
                                        // console.log(item.file_name);
                                    }
                                    else {
                                        tbdt1.push(value);

                                    }
                                });
                            }
                            else {

                            }

                            Loadtbl2();
                            return tbdt1;
                        }

                    },

                },
                "info": false,
                "lengthChange": false,//是否允许用户自定义显示数量
                "bPaginate": false, //翻页功能
                "bFilter": false, //列筛序功能
                "searching": false,//本地搜索
                "ordering": true, //排序功能
                "Info": false,//页脚信息
                "emptyTable": " ",
                "zeroRecords": "No matching records found",
                "columns": ftpcolumns,
                "initComplete": function (settings, json) {
                     
                    ShowHidDel();
                }


            });
        }

        function Loadtbl2() {
            if (tbl2 != undefined) {
                $('#tbl2').DataTable().destroy();
                $('#tbl2').empty(); 
            }
            // 
            tbl2 = $('#tbl2').dataTable({
                "data": tbdt2,
                "columns": ftpcolumns,
                "retrieve": true,
                "info": false,
                "lengthChange": false,//是否允许用户自定义显示数量
                "bPaginate": false, //翻页功能
                "bFilter": false, //列筛序功能
                "searching": false,//本地搜索
                "ordering": true, //排序功能
                "Info": false,//页脚信息
                "emptyTable": " ",
                "zeroRecords": "No matching records found",
                "initComplete": function (settings, json) {
                     
                }
            });

            ShowHidDel();
        }


        </script>



    <script>
        $(document).ready(function () {
            Loadtbl1();
             
            //******************88
            $('.popup-gallery').magnificPopup({
                delegate: 'a',
                type: 'image',
                tLoading: 'Loading image #%curr%...',
                mainClass: 'mfp-img-mobile',
                gallery: {
                    enabled: true,
                    navigateByImgClick: true,
                    preload: [0, 1] // Will preload 0 - before current, and 1 after the current image
                },
                image: {
                    tError: '<a href="%url%">The image #%curr%</a> could not be loaded.',
                    titleSrc: function (item) {
                        return item.el.attr('title') + '<small> </small>';
                    }
                }
            });

            $('.poptest').magnificPopup({
                delegate: 'a',
                type: 'image',
                tLoading: 'Loading image #%curr%...',
                mainClass: 'mfp-img-mobile',
                gallery: {
                    enabled: true,
                    navigateByImgClick: true,
                    preload: [0, 1] // Will preload 0 - before current, and 1 after the current image
                },
                image: {
                    tError: '<a href="%url%">The image #%curr%</a> could not be loaded.',
                    titleSrc: function (item) {
                        return item.el.attr('title') + '<small> </small>';
                    }
                }
            });

           

            $('#tbl1').on('click', 'tr input[name="btndel"]', function () {

               
                var $tr = $(this).parents('tr');
                // event.preventDefault();
                var idx = tbl1
                    .row($(this).parents('tr'))
                    .index();

                var dt = tbl1
                    .row($(this).parents('tr')).data();

                console.log(dt.refid);

                var jsondata = {
                    "cmd": "Delete",
                    "refid": dt.refid
                };
                // alert(idx);

                // $tr.remove();

                if (jConfirm("Are you delete?")) {
                    ajaxTableDel("http://localhost/workflow/HttpServer/ftpserver.ashx", jsondata, idx);
                }
                else {
                    AlertHit("Cancelled...");
                }

            }
            );

            $('#tbl2').on('click', 'tr input[name="btndel"]', function () {


                var $tr = $(this).parents('tr');
                // event.preventDefault();
                var idx = $(this).parents('tr').index();


                tbl2 = $('#tbl2').DataTable();
                var dt = tbl2
                    .row($(this).parents('tr')).data();
                
                console.log(dt.refid);
                var jsondata = {
                    "cmd": "Delete",
                    "refid": dt.refid
                };
                // alert(idx);

                // $tr.remove();

                if (jConfirm("Are you delete?")) {
                    ajaxTableDel("http://localhost/workflow/HttpServer/ftpserver.ashx", jsondata, idx);
                }
                else {
                    AlertHit("Cancelled...");
                }

            }
            );
        });

                  //**********************************
    </script>


    <script>

      
    </script>

 

### 回答1: 针对中文文件名乱码问题,可以在使用jsftp上传或下载文件时,设置文件名编码格式为utf8。具体做法是在连接服务器时,添加一个options参数,将其charset属性设置为utf8,示例代码如下: ```javascript var JSFtp = require('jsftp'); var ftp = new JSFtp({ host: 'ftp.example.com', port: 21, user: 'username', pass: 'password', options: { charset: 'utf8' } }); ``` 通过这种方式,可以确保jsftp能够正确处理中文文件名,避免出现乱码问题。 ### 回答2: 当使用 JSFTP 协议时,中文文件名可能会出现乱码问题。解决该问题的一种方法是使用 UTF-8 编码来传输文件名。 首先,确保服务器端的 FTP 服务器已正确配置为使用 UTF-8 编码。这可以通过设置服务器的语言和编码选项来实现。在 Linux 上,可以通过编辑 /etc/vsftpd.conf 文件来进行配置。将以下选项设置为对应的值: ``` utf8_filesystem=YES ``` 保存并重启 FTP 服务器。 在客户端的代码中,我们需要将文件名进行 UTF-8 编码,以便正确传输。可以使用 JavaScript 的 `encodeURIComponent()` 方法来对文件名进行编码,示例如下: ```javascript let fileName = '中文文件名.txt'; let encodedFileName = encodeURIComponent(fileName); ``` 然后,将编码后的文件名用作 FTP 命令的参数进行传输。例如,使用 `STOR` 命令上传文件: ```javascript ftpClient.put(localFilePath, encodedFileName, (err) => { if (err) { console.error('上传文件失败:', err); } else { console.log('文件上传成功。'); } }); ``` 这样传输的文件名就可以正确识别和保存中文字符了。 需要注意的是,对于一些较旧的 FTP 服务器,可能不支持 UTF-8 编码,此时需要根据服务器的具体情况进行进一步配置和处理。 ### 回答3: 在处理FTP下载或上传中文文件名时出现乱码的问题,可以考虑以下解决方案: 1. 确保服务器端和客户端都使用UTF-8编码:在FTP服务器的配置文件中,设置字符编码为UTF-8。同时,在JS代码中使用encodeURIComponent()函数对中文文件名进行编码,确保传输过程中的正常解析。 2. 使用node.jsftp模块:node.js提供了丰富的FTP模块,如"ftp"、"ftpd"等,这些模块对中文文件名的支持更好。可以借助这些模块来处理中文文件名乱码问题。 3. 对中文文件名进行编码和解码:在发送FTP请求之前,对中文文件名使用encodeURIComponent()函数进行编码。在接收到FTP响应后,使用decodeURIComponent()函数进行解码。这样可以确保传输过程中文件名的正确解析。 4. 修改FTP客户端的字符编码:针对具体的FTP客户端,可以在设置中修改其字符编码为UTF-8或其他支持中文的编码方式。这样可以解决在客户端操作中文文件名时出现的乱码问题。 5. 使用第三方库或插件:有些第三方库或插件(如jQuery-File-Uploadjsftp)会在文件上传或下载时自动处理字符编码,可避免中文文件名乱码的问题。 需要注意的是,以上解决方案适用于不同环境和情况下的中文文件名乱码问题。可以根据具体的需求和环境选取合适的解决方案。同时,建议在操作中文文件名前进行充分的测试和验证,确保能够得到正确的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Farmwang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值