通过 html 和 javascript 在远程服务器上上传文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12763913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Upload files on remote server through html & javascript
提问by Saurabh
I am trying to upload files through html page in our unix based server but I don't know how to take the files on remote server & save files there.
我正在尝试通过基于 unix 的服务器中的 html 页面上传文件,但我不知道如何在远程服务器上获取文件并在那里保存文件。
I write the following code please help me to connect it.
我写了下面的代码请帮我连接它。
<html>
<head>
<script type="text/javascript">
function Upload()
{
var filename = document.getElementById("filename").value;
var storepath = "HOSTURL/Foldername";
}
</script>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data" >
<input type="file" name="filename" />
<input type="submit" value="Upload" onclick="Upload" />
</form
</body>
</html>
回答by Erwin
Why using JavaScript? You can simple use the html form to post your file to the server:
为什么使用 JavaScript?您可以简单地使用 html 表单将文件发布到服务器:
<html>
<body>
<form action="/foo/bar.ext" method="post" enctype="multipart/form-data">
<input type="file" name="filename" />
<input type="submit" value="Upload" />
</form>
</body>
</html>
Change the form action
to the location you want to post the file to.
将表单更改为action
要将文件发布到的位置。
回答by Ner0
PHP would be a better choice for this.
PHP 将是一个更好的选择。
<?php
if( isset( $_POST["Upload"] ) )
{
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['filename']['name']);
if(move_uploaded_file($_FILES['filename']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['filename']['name']). " has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}
?>
<form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<input type="file" name="filename" />
<input type="submit" value="Upload" name="Upload" />
</form>