簡體   English   中英

使用node.js創建和提交表單

[英]Creating and submitting a form using node.js

我是node.js的新手。 我需要創建一個帶有文件的表單並將其提交到url。

怎么做呢?

我找不到關於它的明確信息。

我不想使用表單數據執行此簡單操作,而且它需要一系列依賴模塊。

我使用了nodejs-websocket模塊來創建一個從瀏覽器客戶端偵聽的服務器。

您可以嘗試在服務器端使用express / multer處理文件上傳。 它很簡單。

單擊該按鈕時,請在客戶端使用AJAX請求上載文件。

<form role="form" enctype="multipart/form-data" >
   .....
   <input type="file"/>
<form />


$('#upload_button_id').click(function (event) { 
  $('#form_id').on('submit', function(e) { 
    e.preventDefault(); 

    var formData = new FormData($('form')[0]); 

    $.ajax({
      type:'post,'
      url: '/url/fileupload/', //the URL to your node.js server that has data
      dataType: 'json',
      data: formData, 
      cache: false, 
      contentType: false, 
      processData: false, 
      success: function(response){
         //file was uploaded successfuly 
         alert(response);
      },
      error: function(){
         //Something went wrong
      } 
   });
  });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM