Server not pointing to folder - asp.net

i have a website on server. It has a button which triggers the downloading of a file(.zip/.doc) from that server.
But it is not pointing to that folder/file. How to resolve this issue??
It fails at this particular file...
public static bool ResponseFile(HttpRequest _Request, HttpResponse _Response, string _fileName, string _fullPath, long _speed){
FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}

I have not tried your code but this seems to work for me. you can make the rest dynamic.
string FileName = "test.zip";
string PathFile = "C:\project\download\test.zip";
var fileInfo = new System.IO.FileInfo(PathFile);
Response.ContentType = "application/zip";
Response.AddHeader("Content-Disposition", String.Format("attachment;filename=\"{0}\"", FileName));
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.WriteFile(PathFile);
Response.End();

Related

Download PDF into Webapplication(.Net) from SSRS URL access method

I am trying to download a pdf file from SSRS server to my .net application. I am using URL access method to get byte data from the SSRS-URL (http://SsrsDevServer/ReportServer/Pages/ReportViewer.aspx?%2fMainFolder%2fReports%2fInvoice&rs:Command=Render&). My .net application code is shown below.
Note : by making Client.UseDefaultCredentials = true; and without credentials part, it works as expected only in my local machine, but failing when I published it to DEV server.
WebClient Client = new WebClient();
// Client.UseDefaultCredentials = true;
string strReportUser = "USERNAME";
string strReportUserPW = "PAssword";
string strReportUserDomain = "Domain";
Client.Credentials = new System.Net.NetworkCredential(
strReportUser,
strReportUserPW,
strReportUserDomain);
byte[] myDataBuffer = Client.DownloadData(theURL);
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=\"" + fileName + "\"");
Response.BinaryWrite(myDataBuffer);
Response.Flush();
Response.End();
Appreciate any help.
Thanks!

How to download a file from sub domain which is available on domain in asp.net C#

How to download a file from sub domain which is available on domain in asp.net C Sharp
suppose I have a file abc.doc on example.com
but now I want to download this file abc.doc from admin.example.com ,
how it is possible in asp.net C sharp
I tried lots of code but it is showing is not a valid virtual path.
This is the code which I used.
string strURL = "`http://example.com/resume.doc`";
WebClient req = new WebClient();
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.Buffer = true;
response.AddHeader("Content-Disposition", "attachment;filename=\"" + Server.MapPath(strURL) + "\"");
byte[] data = req.DownloadData(Server.MapPath(strURL));
response.BinaryWrite(data);
response.End();
You are using Server.MapPath on an URL. You can't do that since the URL isn't on your server file system. Remove that and you can download your file.
byte[] data = req.DownloadData(strURL);
Thanks for quick reply Mr. Sami , I have removed as you have suggested by it is showing error http://example.com/resume.doc is not a valid virtual path. My new code is string code is
string strURL = "http://example.com/resume.doc";
WebClient req = new WebClient();
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.Buffer = true;
response.AddHeader("Content-Disposition", "attachment;filename=\"" + Server.MapPath(strURL) + "\"");
response.BinaryWrite(data);
response.End();

Download File using webService

[WebMethod()]
public void GetFileByDocumentNumber(string DocumentNumber)
{
string FilePath = GetFile(DocumentNumber);
string FullPath = ConfigurationManager.AppSettings["FilePath"] + FilePath;
DownloadToBrowser(FullPath);
}
private void DownloadToBrowser(string filePath)
{
FileInfo file = new FileInfo(filePath);
Context.Response.Clear();
Context.Response.ClearHeaders();
Context.Response.ClearContent();
Context.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Context.Response.AddHeader("Content-Length", file.Length.ToString());
Context.Response.ContentType = "text/plain";
Context.Response.Flush();
Context.Response.TransmitFile(file.FullName);
Context.Response.End();
}
I'm using above code for my web service in order to download a file from the server.it's working fine in the local machine but when i host my webservice on a server and try to consume the service it gives the following error
Client found response content type of 'text/plain', but expected 'text/xml'.
whats the reason for this error?
You should have to return a file content via WebMethod.
[WebMethod()]
public string GetFileByDocumentNumber(string DocumentNumber)
{
string FilePath = GetFile(DocumentNumber);
string FullPath = ConfigurationManager.AppSettings["FilePath"] + FilePath;
return File.ReadAllText(FullPath);
}
Try to replace ContentType with application/octet-stream.

File Download inside iframe

I'm using following method to download a file.on a button click inside a iframe.it's working fine in every browser except IE.can some one plz suggest me a sollution
private void DownloadToBrowser(string filePath)
{
try
{
FileInfo file = new FileInfo(filePath);
Context.Response.Clear();
Context.Response.ClearHeaders();
Context.Response.ClearContent();
Context.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Context.Response.AddHeader("Content-Length", file.Length.ToString());
Context.Response.ContentType = "text/plain";
Context.Response.Flush();
Context.Response.TransmitFile(file.FullName);
Context.Response.End();
}
catch (Exception ex)
{
throw ex;
}
}
I would suggest removing the Context.Response.Flush(); line... I don't think it's necessary (as it will happen as part of Context.Response.End(); line), and maybe messing up how the browser receives the file on the next line.
Also, is the file you're transmitting definitely a plain-text file? If not, you'll need to provide a different Context.Response.ContentType();
You have most probably missed some HTTP response headers required by IE.
There is KB on microsoft web.
Also take a look at similar question on SO : PHP script to download file not working in IE
You could try something like this :
Context.Response.Buffer = true;
Context.Response.ContentType = "application/pdf";
Context.Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName );
Context.Response.OutputStream.Write(dataBytes ,0,FileContentLength);
Also try adding this to your aspx page :
<%# Page aspCompat="True" other attributes %>
For Understanding I m using Dataset,
For download as a Excel File
Use Namespace:
using System.IO;
using System.Data;
DataSet ds = new DataSet("Table");
ds.Tables.Add("Table1");
ds.Tables[0].Columns.Add("Field1");
ds.Tables[0].Columns.Add("Field2");
ds.Tables[0].Columns.Add("Field3");
ds.Tables[0].Rows.Add();
ds.Tables[0].Rows[0][0] = "1";
ds.Tables[0].Rows[0][1] = "2";
ds.Tables[0].Rows[0][2] = "3";
HttpResponse response = HttpContext.Current.Response;
// first let's clean up the response.object
response.Clear();
response.Charset = "";
// set the response mime type for excel
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=sample.xls");
// create a string writer
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// instantiate a datagrid
DataGrid dg = new DataGrid();
dg.DataSource = ds.Tables[0];
dg.DataBind();
dg.RenderControl(htw);
response.Write(sw.ToString());
response.End();
}
}
For Download as a Word File
Replace
response.ContentType = "application/msword";
response.AddHeader("Content-Disposition", "attachment;filename=sample.doc");

How can I prompt a user to open or save a PDF file returned by a .aspx file?

I have a Flex application that calls a .aspx page on our webserver, which builds a PDF or Excel File. Right now, I am using navigateToURL() to call the file, and this works fine when the output file is built successfully. However, if there is an error, or timeout, there is no way of knowing this in the Flash movie.
I am trying to use URLLoader instead, so that I can listen for an HTTP Status Code, and know when the load is complete, however, the URLLoader simply returns the bitmap data, with nothing prompting the user to open or save the output file. Is there anyway to do this?
Here are both versions of my ActionScript code:
private function buildFile():void
{
var variables:URLVariables = new URLVariables();
variables.rptName = "report1";
variables.rptFormat = "PDF";
var request:URLRequest = new URLRequest();
request.url = "/buildFile.aspx";
request.method = URLRequestMethod.POST;
request.data = variables;
var loader:URLLoader = new URLLoader();
loader.addEventListener(HTTPStatusEvent.HTTP_STATUS,
httpStatusHandler);
loader.load(request);
}
private function buildFile():void
{
var variables:URLVariables = new URLVariables();
variables.rptName = "report1";
variables.rptFormat = "PDF";
var request:URLRequest = new URLRequest();
request.url = "/buildFile.aspx";
request.method = URLRequestMethod.POST;
request.data = variables;
navigateToURL(request, "_self");
}
FYI, here is the block of code that currently outputs the PDF or Excel file:
System.Web.HttpContext httpC;
httpC = System.Web.HttpContext.Current;
httpC.Response.Clear();
httpC.Response.ClearHeaders();
httpC.Response.Expires = 0;
switch (ReportFormat)
{
case "Excel": Response.ContentType = "application/vnd.ms-excel"; break;
case "PDF": Response.ContentType = "application/pdf"; ; break;
}
Response.AddHeader("Content-disposition", "attachment;filename=" + fileName);
BinaryWriter binaryWriter = new BinaryWriter(httpC.Response.OutputStream);
binaryWriter.Write(result);
You server should send "application/octet-stream" header when user requests that PSD file.
With ASP.NET it could look like this:
Response.ClearContent();
Response.ClearHeaders();
// with this header user will be promted to open or download file
Response.ContentType = "application/octet-stream";
// with this header, you will suggest to save file with "test.pdf" name
Response.AddHeader("Content-Disposition", "attachment; filename=test.pdf");
Response.WriteFile(Server.MapPath("~/test.pdf"));
Response.Flush();
Response.Close();
Also take a look here: Downloading files in Flex using the FileReference class
Also set the content-disposition to "attachment" in the aspx page.

Resources