Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Web Services / June 2005

Tip: Looking for answers? Try searching our database.

Web Service Sample for Downloading Files?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ken - 26 May 2005 20:32 GMT
Can anyone point me to some info, which gives me an idea of how to go
about making an ASP.NET web service to download files

I plan to access the web service from a C# Windows App.

Thanks.
Ziga Jakhel - 31 May 2005 10:06 GMT
Well... if you are transferring files, you are basically transfering bytes. So a simple way would be to simply return a byte array, which you can then save on the client.

Example:

[WebMethod]
public byte[] GimmeMyFile(string fileName)
{
//Do whatever magic you need to do to get the file and transfer it into a bytestream.
//A simple example is this:
System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open);
byte[] barr = new byte[fs.Length];
fs.Read(barr, 0, fs.Length);
return barr;
}

If you have extremely large files or so many requests you would clog up your ws server memory, you can implement your own mechanism for chunking the file and transferring inidividual chunks. An example:

[WebMethod]
public byte[] GimmeMyFile(string fileName, int offset, int length)
{
//Do whatever magic you need to do to get the file and transfer it into a bytestream.
//A simple example is this:
System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open);
int len = fs.Length;
//If you are requesting bytes beyond file length, abort.
if (offset > len) return new byte[0];
//else get the file
if (len < offset + length)
{
len = len - offset; //if you are at the end of file, you need less bytes.
}
else
{
len = length;
}
byte[] barr = new byte[len];
fs.Read(barr, offset, len);
return barr;
}

This way you would simply keep calling the method with increasing offsett (by the chunk size) until you get back a chunk that's shorter then the chunk size you requested. Then you know you've reached the end of the file.

If you have larger files or narrow communication channels you may consider compressing the file before transfer (and then uncompressing it again on the client); for this Google up SharpZipLib. Works decently fast.

Enjoy!

Regards,

Sigmund Jakhel
MCSD.NET

> Can anyone point me to some info, which gives me an idea of how to go
> about making an ASP.NET web service to download files
[quoted text clipped - 4 lines]
>
> *** Sent via Developersdex http://www.developersdex.com ***
Chad Z. Hower aka Kudzu - 31 May 2005 13:21 GMT
Ken <krcourville_atsign_msn_period_com> wrote in news:u#KlgmiYFHA.3356
@TK2MSFTNGP15.phx.gbl:
> Can anyone point me to some info, which gives me an idea of how to go
> about making an ASP.NET web service to download files
>
> I plan to access the web service from a C# Windows App.

If you are only downloading files, HTTP is a better choice.

--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
     "Programming is an art form that fights back"

Blog: http://blogs.atozed.com/kudzu
Ken - 31 May 2005 14:35 GMT
Thanks for the responses.

I was researching this a bit more and found mention of using DIME over
http.

The byte array makes sense.  If http is better, how so?  Can you point
me to sample code?

My basic goal here is to make an application update service where the
remote client will query the web service for new files then download the
files it needs.
Chad Z. Hower aka Kudzu - 03 Jun 2005 10:23 GMT
Ken <krcourville_atsign_msn_period_com> wrote in news:OwNUOWeZFHA.3908
@TK2MSFTNGP15.phx.gbl:
> The byte array makes sense.  If http is better, how so?  Can you point

Because if you arent requiring all the extras SOAP provides, its always best
to use the simplest solution that meets your needs, and HTTP can easily
download a file.

> me to sample code?

No, but its very easy.

> My basic goal here is to make an application update service where the
> remote client will query the web service for new files then download the
> files it needs.

What kind of query will you be submitting?

--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
     "Programming is an art form that fights back"

Blog: http://blogs.atozed.com/kudzu

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.