Solved

Windows service for automatic file uploading to web site..

Posted on 2004-09-20
3
334 Views
Last Modified: 2012-05-05
I would like to be able to write a Window's service application that is able to automatically upload files to a web site.  Basically the service will poll a directory for new files to upload to the web.  I have the process of polling for files complete, but I cannot figure out how to upload them in the background.  There is a page on the web site that is simply a form with a text box for the filename and an update button.

I have tried using the AxWebBrowser control, but it does not seem to work from within a service (maybe I am just using it wrong).

Any ideas would be great, Thanks!
0
Comment
Question by:stu_pb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
ajitanand earned 250 total points
ID: 12109440
hello,

I believe if the web server script is your own(you have control on it), then you dont even need to use any third party software.

The following code (pure .Net) helps you upload a file in just few lines!  The only thing is that you can not control the name of the FormField that is designated to the uploaded file.
(add a refernce to System.Web.dll to your project)

----------------------------------------
Public Sub Upload() 'Much Neat than using the AxWebBrowser!!!
      Dim path As String = "D:\\clientsdata\\genesis\\webs\\accept.asp"
      Dim url As String = "http://localhost/accept.asp"

      Dim client As System.Net.WebClient = New System.Net.WebClient()
      Dim bytes() As Byte = client.UploadFile(url, "POST", path)
      client.Dispose()

      'Examine response to see if upload was successful.
      'this response is the standard HTML output thrown by the web script that is accepting the file upload
      'In my example I assume that the scripts returns "success" if file is uploaded

      Dim response As String = System.Text.Encoding.ASCII.GetString(bytes)
      Console.Out.Write(response)
End Sub
----------------------------------------
This HTML control name "MIGHT" be relevant since the fileupload control in the html form that your script that accepts file upload on the webserver, might expect a specific Name.

for e.g. the following html form uploads a file under the name MyFile:

<form.............>
<input type=file name="MyFile">
<input type=submit>
</form>

And if your script specifically wants the name of the input field to be exactly MyFile then you can not control it through my code above. To make it work, you need to also make sure that your upload script can be made to accept whatever field uploads the file.

------------------------

Regarding AxWebBrowser Control - Make sure that the same problem is not happening with this control as well.

-----------------------

If you can post your upload script + html file that uploads the file on the server, I can check it. If that is fine, we will then check if your webservice is working and configured properly.

-----------------------
Final Note (Tip): usually to add background processes through Services, you need to make the application multithreaded most of the times.

----------------------

Post your inputs over th above comments, and I will reply specific solutions.

warm rgds,
Ajit Anand

0
 
LVL 5

Expert Comment

by:ajitanand
ID: 12109454
One thing!

AxWebBrowser control is something that exposes UI. Mind it that using UI enabled things in Windows Service is not just the right thing. It needs to be something in background! No UI at all anyhow! So AxWebBrowser is not the right way...

If my above .net based solution does not help, you need another UI less code that uploads files through HTTP.

rgds,
Ajit Anand
0
 
LVL 3

Author Comment

by:stu_pb
ID: 12110906
Thanks for your help, this works perfectly for my situation.
0

Featured Post

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question