使用ftp和electronic上传文件

时间:2019-05-21 09:46:01

标签: node.js ftp electron

我是stackoverflow的新手。我已经开始了一个电子项目,我想在其中实现用户使用ftp上传文件的功能。我已经用Google搜索了一下,并且找到了一个满足我所有需求的不错的节点模块jsftp。问题是我是电子和节点编码的新手,而且我不知道如何管理文件上传。我尝试使用标准文件输入,但未成功。我的问题是,如何实现用户的文件选择,然后将所选文件上传到ftp服务器? 这是我的代码段:

HTML

  <!-- Upload form row -->
  <div class="row justify-content-center" id="">

    <div class="col-8">
      <form method="POST" action="/process-form" enctype="multipart/form-data" id="file-uploader">
        <div class="form-row">

        <div class="col-6">
          <label for="file_name">File name</label>
          <input type="text" class="form-control" id="file-name" name="file_name" placeholder="Nome file" />
        </div>

        <div class="col-6">
          <label>Select file</label>
          <div class="custom-file">
            <input type="file" class="custom-file-input" name="uploaded_file" id="customFile" />
            <label class="custom-file-label" for="customFile">Seleziona file</label>
          </div>
        </div>

        <div class="col-12">
          <label for="file_description">Description</label>
          <textarea class="form-control" name="file_description" placeholder="Descrizione"></textarea>
        </div>

        <div class="col-12">
          <button type="submit" name="upload_file" class="btn btn-link" id="process-form">UPLOAD</button>
        </div>

        </div>
      </form>
    </div>

  </div>

JS代码:


<!-- init app and xhr requests-->
<script type="text/javascript">
const jsftp = require('jsftp');
const bsCustomFileInput = require('bs-custom-file-input');
let ftp;

$(document).ready(function(){

  $('#login-modal').modal('show');

  $('#login-form').submit(function(e){


    e.preventDefault();
    var host = $('#host-field').val();
    var user = $('#user-field').val();
    var pass = $('#password-field').val();

    ftp = new jsftp({
    host: host,
    port: 21, // defaults to 21
    user: user, // defaults to "anonymous"
    pass: pass, // defaults to "@anonymous"
    });

    ftp.list('public_html', (err, res) => {
       console.log(res);
    });

    console.log(ftp);

  });

  bsCustomFileInput.init();

   $('#file-uploader').submit(function(e){
     console.log(ftp);
      e.preventDefault();
      var formData = new FormData($('#file-uploader')[0]);
      var filename = $('#file-name').val();

      ftp.put(formData, "mysubdomain.testing.net/"+filename+".jpeg", err => {
        if(!err){
          console.log("File transferred successfully!");
        }
        else{
          console.log(err);
        }
      });
   });


});
</script>

0 个答案:

没有答案