473,386 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

download a word document from server using c#.net

iam working in a project where i need to provide a link to user such
that he can download a word Document from server (not my local pc).iam
able to do this from my localhost ,but from other pc i need to use
System.Net.WebRequest but i donot know how to use it.

code for access from localhost:
string filename="anjit.doc";

// if (filename != "")
//
// {
//
// string path = Server.MapPath(filename);
// System.Net.WebRequest
//
// System.IO.FileInfo file = new
System.IO.FileInfo(path);
//
// if (file.Exists)
//
// {
//
// Response.Clear();
//
//
Response.AddHeader("Content-Disposition", "attachment;
filename=" + file.Name);
//
//
Response.AddHeader("Content-Length", file.Length.ToString());
//
// Response.ContentType =
"application/octet-stream";
//
//
Response.WriteFile(file.FullName);
//
// Response.End();
//
// }
//
// else
//
// {
//
// Response.Write("This file does
not exist.");
//
// }
i think i have to use below code but it is not working
System.Net.WebRequest myRequest
=System.Net.WebRequest.Create("http://20.198.118.149/anjit/a.txt");
Console.WriteLine(myRequest);

Oct 10 '06 #1
1 2052
Hi nalla,

Try something like this instead

using System.Web;

WebRequest myRequest =
WebRequest.Create("http://20.198.118.149/anjit/a.txt");
WebResponse myResponse = myRequest.GetResponse();

From the response you get a ResponseStream. For a text file you can use a
StreamReader

StreamReader sr = new StreamReader(myResponse.GetResponseStream()); //
using UTF8 encoding
string text = sr.ReadToEnd();

Console.WriteLine(sr.ReadToEnd());
Even simpler is using a WebClient.DownloadFile/DownloadData, but if you
need more control, use WebRequest, or HttpWebRequest.
--
Happy Coding!
Morten Wennevik [C# MVP]
Oct 10 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Dorsa | last post by:
HI, Could you please tell me the error in here. I am trying to open an XML file from a link. Response.Clear() Response.Expires = 0 Response.BufferOutput = False Response.ContentType =...
3
by: ACaunter | last post by:
Hi there, when one of my pages loads up, the download dialog box pops up to download a file.. but the window turns all white and even after you download the file, the window stays white.. what...
4
by: Kash | last post by:
Hi everybody: I've developed a web application running on a 2003 server not on the web but on extranet environment of a small Firm (less than 10 users). I've taken benefit of Office Primary...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
0
by: Jordan | last post by:
Hi. Hoping I can get some advice... I have an XML format Word document stored on the server. When I open it from the server, it opens fine in Word. When I attach it to an email, and send it to a...
0
by: robwahl | last post by:
Hi, I have a members only area of a site (using ASP and MS Access) where I need users to be able to either view or download reports (PDF or MS Word doc - doesn't matter which). I want to store...
4
by: david | last post by:
I am getting the following error: Error Type: ADODB.Field (0x800A0BB9) Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another. When this code...
0
by: Srinivas Dammalapa | last post by:
Sir, I am D.Srinivas. I am working on JobPortal Website(www.tlabsinc.com). I am downloading MS-Word document using ASP.NET 2.0 & C# 2.0. I am using IE (Internet Explorer). In my...
2
by: allan.s.palmer | last post by:
Hello, I have an asp.net pop up that is generating a Word document real time and sending it to the user by sending the appropriate response headers. When it is run on our development environment...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.