?
Solved

Windows service for automatic file uploading to web site..

Posted on 2004-09-20
3
Medium Priority
?
339 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 1000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

850 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