简体   繁体   English

将文件从本地存储上传到具有chrome扩展名的服务器

[英]uploading file from local storage to server with a chrome extension

How can I upload a file from users local storage to my web server with chrome Extension. 如何使用chrome Extension将文件从用户本地存储上传到我的网络服务器。 I used a simple input tag of type file and when user clicks on it, file browser opens and the extension pop up disappears and nothing is uploaded. 我使用了一个简单的类型为file的输入标签,当用户单击它时,文件浏览器打开,并且扩展名弹出窗口消失并且没有上载任何内容。 I am using google chrome on Ubuntu 15.04. 我在Ubuntu 15.04上使用google chrome。

In my popup.html which loads when icon is clicked : 在我的popup.html中,当单击图标时会加载它:

<form method="post" action="" id="smsCampForm">

  <input type="text" name="recipent_number" id="recipent_number" placeholder="Recipent numbers separated by commas" class="form-control" required>
  <input type="file" name="fileToUpload" class="form-control" placeholder="Upload Excel File">
  <select name="listToSendSms" id="listToSendSms"></select>
  <textarea name ="message_content" rows="5" maxlength="120"  id="message_content" placeholder="Message maximum 120 characters" class="form-control"  required></textarea>
  <input type="submit" id="smsSubmit" name="submit" value="Send SMS" class="btn btn-success"></input>
</form>

And I have a javascript file : 而且我有一个javascript文件:

$("#smsCampForm").submit(function(event){
document.getElementById("smsSubmit").value = "Sending SMS...";
var formData = new FormData(this);
formData.append("chromeExtensionRequest","1");
$.ajax({
  url : myurl,
  type : 'POST',
  data : formData,
  processData : false,
  contentType : false,
  success : function(data,status){
    $("#smsResponse").html("<div class='alert alert-info'>"+data+"</div>");
    document.getElementById("smsSubmit").value = "Send SMS";
  }
});
event.preventDefault();
});
});

Since a popup is completely destroyed when it loses focus, your file picker basically doesn't trigger anything - all code is unloaded. 由于弹出窗口失去焦点时会被完全破坏,因此文件选择器基本上不会触发任何内容-所有代码均已卸载。

There is no way to prevent this behavior; 没有办法防止这种行为。 therefore, you should offload this to something else, like a separate page loaded in a tab or the background page. 因此,您应该将其卸载到其他内容,例如在选项卡中加载的单独页面或背景页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM