7

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.

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>
3
  • 2
    Aside from the fact that you haven't set the action on your form, what you've got should allow you to upload a file to your server (the javascript you have is completely unnecessary). At that point it's all server-side processing. What are you using on your server? Commented Oct 6, 2012 at 21:19
  • 1
    If you really do need to use Javascript (feedback when uploading etc) then dont reinvent the wheel - use one of the many javascript file uploaders like this one. But still easier to just post the file to a server using traditional methods. Commented Oct 6, 2012 at 21:19
  • I am not able to save file on the specified location. Please let me know how can i do that. Commented Oct 7, 2012 at 15:16

2 Answers 2

2

Why using JavaScript? You can simple use the html form to post your file to the server:

<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.

Sign up to request clarification or add additional context in comments.

7 Comments

I am using this & when I test on my local machine it only take me on the action URL & not upload any file there.
I am not able to save file on the specified location. Please let me know how can i do that.
What language do you use php, asp.net etc.? And maybe it's better to create a new question.
I need to work through HTMl & javascript. You can use Jsp file also.
I need to save my file through HTML script on a particular location. Please let me know how can i save my file on my remote location.
|
2

PHP would be a better choice for this.

<?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>

1 Comment

I only need too do that with HTML & javascript... thanks but no need of PHP code. It's goog if You can help me through HTML & javascript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.