Solved

Windows service for automatic file uploading to web site..

Posted on 2004-09-20
3
328 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now