Auto Upload a file [duplicate]

2019-09-25 06:31发布

This question already has an answer here:

I have a bitmap file at a location C:/Demo/HM.bmp I want to upload this file using a single button or without using input type="file". In other words, as soon as I click on a button, I want the file to uploaded using the file location as given above instead of manually selecting the file from a window(which happens in case of input type="file"). Please Help.

Suggest a solution in Javascript.

2条回答
虎瘦雄心在
2楼-- · 2019-09-25 06:54

You cannot do this from a web page.

Here is a rewritten WSH which may put you in the right direction. It has NOT been tested

Your server may have to allow PUT for this to work and the page has to be saved with extension .HTA and opened from harddisk, so it will NOT work from a web page

<script>
function upload() {
  var srcFolder = "C:\\Demo\\"; // note the escaped backslashes
  var strUserID = "MyID";
  var strPassword = "APasswordGoesHere";
  var strURL = "https://www.theuploadwebsite.com/puthere/";

  var HTTP = WScript.CreateObject("Microsoft.XMLHTTP");
  var fso = CreateObject("Scripting.FileSystemObject");
  var folder = fso.getfolder(srcFolder);
  for (file in folder.Files) { // or remove the loop if only one file
    if (fso.GetExtensionName(File)=="BMP"){
      var objStream = CreateObject("ADODB.Stream");
      objStream.Type = 1;
      objStream.Open(); 
      objStream.LoadFromFile(srcFolder & fso.GetFileName(File));
      HTTP.open("PUT", strURL & fso.GetFileName(File), False, strUserID, strPassword) 
      WScript.Echo("Now uploading file " & fso.GetFileName(File));
      HTTP.send(objStream.Read);
      WScript.Echo("Uploading complete for file " & fso.GetFileName(File));
      // fso.DeleteFile(File)
    }
  }
  WScript.Echo("All files uploaded.");
  HTTP = null;
}
</script>
<button onclick="upload()">Upload</button>
查看更多
▲ chillily
3楼-- · 2019-09-25 07:21

This cannot be done in JavaScript (thankfully).

It would represent a serious security hole in web browsers if it was possible for the website to inspect and automatically upload files from the user's local file system...

查看更多
登录 后发表回答