Solved

Windows service for automatic file uploading to web site..

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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