Web Services - File Transfer

Hi All,

     I am using VS 2005 (BETA 2) and a Web Service to upload/download files.  I am trying to use the My.Computer.Network.UploadFile and My.Computer.Network.DownloadFile methods.  In essence, I have a file (test1.exe) that resides in the same directory as the web service, and would like to save it to a remote computer when the user calls my method.  It gives me the following error:

System.Net.WebException was caught
  Message="The remote server returned an error: (403) Forbidden."
  Source="System"
  StackTrace:
   at System.Net.WebClient.DownloadFile(Uri address, String fileName)
   at Microsoft.VisualBasic.MyServices.Internal.WebClientCopy.DownloadFile(Uri address, String destinationFileName)
   ...
   
at Service.DownloadFileToClient(String source, String destination) in C:\Documents and Settings\TEST PC\my documents\visual studio 2005
   WebSites\AutoUpDateServerWebService\App_Code\Service.vb:line 17

     Anyone has any ideas I can see that its security related, but don't know what

     Regards,

         Giovanni P.



Answer this question

Web Services - File Transfer

  • DavidPal

    Some example C# .NET v2.0 code to return a file from a WebService.

    using System.Xml;
    using System.Xml.Schema;
    using System.Xml.Serialization;

    [WebService(Namespace = "http://somenamespace")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class FileService : System.Web.Services.WebService
    {
        [WebMethod]
        public GetFileResponse GetFile(string fileName)
        {
            GetFileResponse response = new GetFileResponse();
            response.FileName = fileName;
            String filePath = AppDomain.CurrentDomain.BaseDirectory + @"App_Data\" + fileName;
            response.FileContents = File.ReadAllBytes(filePath); 
            return response;
        }
    }

    [XmlRoot("getFileResponse", Namespace = "http://somenamespace")]
    public class GetFileResponse
    {
        [XmlElement("fileName", IsNullable = false)]
        public string FileName;

        [XmlElement("fileData", IsNullable = false)]
        public byte[] FileContents;
    }

    Mark
    WSE PM



  • Subasree

    If the need to download files, is only to update your application, You can try using the ClickOnce technology.

    You can customize when the users will download updates very easily using this.
    It would perform much better, than downloading files in the web service

  • SiroroS

    If your webservice is trying to send a file back to a client it must send it in a format that the client will understand - in this case binary byte array. This is also generally true even when it is vice versa - e.g. the client is sending the webservice a file.

  • vze2dytt

    Just another thought for your scenario:

    Use the Web Service just to transfer information about the files that need to be downloaded. Use a FTP Server to download the actual files. I think that will perform better than downloading files in Web Services.

    To transfer files in Web Service, as in the sample code above, would hog your server memory.

    For the web service to transfer data like that, the complete file needs to be read in memory, before it can be transferred to the client. Some big files and a couple of users doing it simultaneously, can lead to scalability issues

  • Sean Gerety

    Hi Mark

    Thanks for your helpfulness.

    I am very new at Web Services.

    What the duty of web services in a project, which operations are they used for mostly

    Can you advise me clear and simple source for Web Services

    and lastly in this article what must i do to use your code for downloading file

    If I want to use Web Services;I must add something(I dont know may be web reference..) project first.

    And what must we write for "somenamespace"

    Thanks

    Fatih AK


  • Randall Ulloa

    Hi Mark,

       Thanks for the code sample, will try it out and let you know.  Some questions:

       1.  Does ReadAllBytes read all the file into memory before   I ask this as I may have files in the hundreds of MB's in size X number of users downloading X number of files simultaneously.
       
       2.  Are there any security issues that I should be aware of and take into consideration when performing file Uploading/Downloading using a Web Service.

       3.  Can I use the Net.DownloadFile and Net.UploadFile to accomplish the same

       4.  How does one go about deciding whether to use the Net.UploadFile/Net.DownloadFile methods or use MOTM technology   What is the deciding factor

       In essence, I would ideally have a tray application (on several desktop PC's) poll a Web Service (interval-based), determine whether a new version of my application is available (through a manifest file), and download all new files.


       Regards,

          Giovanni P.

  • Antao Almada

    I have never used these methods before.
    Just looking at the help for these suggests, that they might be what you are looking for.

    It would mean, you will not be using the Web Service to download files. Rather just for transferring information about changed files.

  • KDavie

    Hi,

       I was thinking of using .Network.UploadFile/.Network.Download file methods   Would those work

       Regards,

          Giovanni

  • GMc

    Hi,

       Unfortunately, the application I am building to download the needed files is only Part I of a multi-step process.  the problem is that I am using this method to update a legacy VB 6 application, and therefore cannot use ClickOnce technology.

         Regards,

              Giovanni

  • Web Services - File Transfer