Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

uplaod file to http server sing power shell 2

Status
Not open for further replies.

sahasudipta

Programmer
Apr 9, 2018
4
US
I want to transfer a XML file to http server ( . I wrote scripts for this and it's running but actually it's not uplaoding the files to http server .

My code

$sourceFilePath = "C:\Temp\NEWFOLDER\ABCDEF.XML"
$siteAddress = "$urlDest = "{0}/{1}" -f ($siteAddress, "ABCDEF.XML");
$UserName = "XXXXX"
$Password = "XXXXX"

function uploadFile()
{

Param ([string] $sourceFilePath,
[string] $siteAddress ,
[string] $urlDest,
[string] $UserName,
[string] $Password)

$webClient = New-Object System.Net.WebClient;
$webClient.Credentials = New-Object System.Net.NetworkCredential($UserName,$Password);

("*** Uploading {0} file to {1} ***" -f ($sourceFilePath, $siteAddress) ) | write-host -ForegroundColor Green
$webClient.UploadFile($urlDest, "PUT", $sourceFilePath);



}

uploadFile $sourceFilePath $siteAddress $urlDest $UserName $Password


ERROR

Exception calling "UploadFile" with "3" argument(s): "The remote server returned an error: (503) Server Unavailable."At
C:\ABCDEF-documents\upload-file.ps1:40 char:13
+ $webClient.UploadFile($urlDest, "PUT", $sourceFilePath);
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: :)) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
Can anyone help me on this . Any clue will be helpful.
 
Uploading files with an HTTP form submitted by POST already needs a server-side script receiving the file, it arrives in a temp dir and needs to be copied to a destination location in that case. I'm quite sure the same applies to a PUT request, there is no default web server action just taking the file in and putting it where you specify it with the URL.

I'd go for FTP STOR, not HTTP PUT.

Bye, Olaf.
 
thanks for reply :) . I am very much new to this . So you are telling me that I should use HTTP STOR process for this? Any kind of pdf url,post regarding will be helpful .
 
I agree with what Olaf has said.

NO you do not have to use an HTTP <Anything> to merely transfer a file from your site to another site.
In some cases you use an HTTP PUT/POST and in others you use FTP/SFTP - it all depends on what the receiving site is providing for your use.

It sounds like the first thing you need to do is to find out specifically what the remote site is offering you.
* An FTP/SFTP site
If so, find out specifically if you are to send your file as FTP or SFTP​
* A Web Service to receive HTTP PUT/POST transfers.
If so, find out specifically if it needs you to send your file as an HTTP PUT or HTTP POST​
* or what?

Then, once you know that and have received the appropriate credentials for it, you can then determine what you code needs to do.

Good Luck
JRB-Bldr
 
thanks for reply :) .. My web server is http .. and I want to just upload my local xml file to http server .... that's all. how can I achieve that using power shell.. creating web service using power shell which will upload files to http server..
 
OK. So your remote server is providing you a Web Service using one of the HTTP protocols and not an FTP/SFTP site.

Now you need to find out if that Web Service is supporting HTTP PUT's or HTTP POST's because your code will need to differ somewhat depending on which it is.

And you will need to find the precise protocol syntax that the Web Service expects in order to handle the file transfer.

Things like Destination folder, etc. are either 'hard coded' into the Web Service's code or they are passed to it as one of the Parameters of the POST/PUT URL syntax.

Also, as I said above, if your Remote HTTP Web Service has special authentication credentials you need to get that information as well.

For example the last one I did was for sending XML files to a State Agency.
Their Web Service had its own handling of the Destination folder (it was NOT passed) and it required the password to be base64 encrypted.
Each Web Service may very likely have been created to have its own unique requirements.

NOTE: I have never used 'Power Shell' so I can't help you with that, but I have created a few VFP applications that run HTTP file transfer routines.

Good Luck
JRB-Bldr
 
You need to change from HTTP to FTP, you need an FTP url, not HTTP. It's far easier to FTP a file than to upload it via HTTP.
Your hosting will include FTP access and what you upload via FPT will be available via browser (HTTP).

Bye, Olaf.
 
A while back, I was given some advice on how to upload a file or files using FTP.

The thread for reference can be found here: thread184-1766403

I needed to run an automated process via an app and now use the following:

Create a file called sendftp.prg and add the following (replacing details with your own FTP)

Code:
WAIT "Uploading file, please wait...." WINDOW NOWAIT

cCrLf = CHR(13) + CHR(10)
* --- Build a String of what needs to be in the SEND.TXT file ---
* --- In this way VFP can fill in the appropriate values from the 'mjobupload' variables ---

mfileone="thenameofyourfile"
mfiletwo="anotherfileifyouhaveone"

cSendTextStr = "OPEN ftp.yoursite.com" + cCrLf ;
          + "ftpusernumber-X" + cCrLf ;
          + "password" + cCrLf ;
          + "binary"  + cCrLf ;
          + "send " + fileone + cCrLf ;
          + "send " + filetwo + cCrLf ;
          + "close"  + cCrLf ;
          + "bye"  + cCrLf ;
          + "exit"

* --- Now Write String Out to File (SEND.TXT) ---

STRTOFILE(cSendTextStr, "send.txt")

RUN ftp  -i -s:send.txt

WAIT "File uploaded...." WINDOW NOWAIT
RETURN

Create a file called send.txt and include the below obviously changing the settings to your FTP

Code:
OPEN ftp.yoursite.com
ftpusernumber-X
password
binary
send fileone
send filetwo
close
bye
exit

Needless to say, files are uploaded to a space on our server where they are stored for later use as downloads.

If this is what you're trying to achieve, then hope it assists.

Thank you

Steve Williams
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top