is not a valid virtual path error - asp.net

This is my code for downloading text file. But the server.transfer method is not able to resolve that path. It is giving "is not a valid virtual path error"
string filePath = #"D:/BCPResult/Cust_File.t`enter code here`xt";
Response.ContentType = "text/plain";
Response.AppendHeader("content-disposition",
"attachment; filename=" + filePath);
Response.TransmitFile(Server.MapPath(filePath));
Response.End();
Please guide me...

If your file path is not related to server you do not need Server.MapPath.
Also if you run your code in windows, path separator is \, not /.
This code must work:
string filePath = #"D:\BCPResult\Cust_File.txt";
Response.ContentType = "text/plain";
Response.AppendHeader("content-disposition", "attachment; filename=" + filePath);
Response.TransmitFile(filePath);
Response.End();

Use '\' (backslash) instead '/'.
string filePath = #"D:\BCPResult\Cust_File.txt";
or
string filePath = "D:\\BCPResult\\Cust_File.txt";

If You Are trying to access a file from the Remote URL or External URL like (https://www.example.com/)
then you have to Use for the web client like this
string url= "this is the URL of the file from where you want to access the file"
System.Net.WebClient webClient = new System.Net.WebClient();
byte[] bytes = webClient.DownloadData(url);
if (mimetype != null)
{
context.Response.ContentType = mimetype;
}
else
{
context.Response.ContentType = "Application/pdf";
}
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
context.Response.BinaryWrite(bytes);
context.Response.End();

Related

'http:/***.168.**.8:***/UploadedFiles/CustomerKYC/Photo/134_26581.jpg' is not a valid virtual path

I am getting the exception http:/.168.11.8:/UploadedFiles/CustomerKYC/Photo/134_26581.jpg' is not a valid virtual path when I write either WriteFile or TranferFile in the following code. Please give me a code fix.
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AppendHeader("content-disposition", "filename=" +NavigateURLID.Value);
Response.WriteFile(Server.MapPath(url));
Response.Flush();
Response.End();
I need the file from the url to be downloaded. They are all image files only(jpg)
Try this code will help
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
Response.WriteFile(filePath);
Response.End();
Reference url: https://www.aspsnippets.com/Articles/Upload-and-Download-files-from-Folder-Directory-in-ASPNet-using-C-and-VBNet.aspx
I found out the answer after searching in Internet. The code is.
WebClient req=new WebClient();
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.Buffer= true;
response.AddHeader("Content-Disposition","attachment;filename=\"" +strURL + "\"");
byte[] data=req.DownloadData(strURL);
response.BinaryWrite(data);
response.End();

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();

File Download from remote server

I am facing a strange problem.
I have a web page(Filelibrary.aspx) to download some word/pdf files, I can download the files from local machine and direct link from my local pc. but we have a server and we are usualy accessing the site by login into the server as remote desktop. If we try to download word/pdf files from remort desk top it is downloading the web page "Filelibrary.aspx" instead of the word/pdf. we are using https. I could able to download it before when it was http.
my code:
String strFile = String.Empty;
String[] filename;
strFile = Server.MapPath(ConfigurationManager.AppSettings["TemplatePath"].ToString()) + FileName;
filename = strFile.Split('\\');
Response.Clear();
Response.Buffer = true;// Read the original file from diskFileStream *
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename[filename.Length-1]);
String ext = String.Empty;
if (strFile.IndexOf(".") > 0)
ext = (strFile.Split('.'))[1];
if (ext.ToLower() == "txt")
Response.ContentType = "text/html";
if (ext.ToLower() == "xls")
Response.ContentType = "xls/html";
if (ext.ToLower() == "pdf")
Response.ContentType = "application/pdf";
////combine the path and file name
if (File.Exists(strFile))//open the file and process it
{
FileStream sourceFile = new FileStream(strFile, FileMode.Open, FileAccess.Read);
long FileSize;
FileSize = sourceFile.Length;
Response.AddHeader("Content-Length", FileSize.ToString()); //*
byte[] getContent = new byte[(int)FileSize];
sourceFile.Read(getContent, 0, (int)FileSize);
sourceFile.Close();
Response.BinaryWrite(getContent);
}
Common.UserPageAudit(Session["User"].ToString(), "Download Templates", Session["ROLE"].ToString(), strFile + " Template downloaded");
Can anyone help me to resolve this strange issue..?
Thanks you friends...
at last I found the Problem.
I was not clearing the Header.. I have changed my code as below.
String strFile = String.Empty;
String[] filename;
strFile = Server.MapPath(ConfigurationManager.AppSettings["TemplatePath"].ToString()) + FileName;
filename = strFile.Split('\\');
// Added these two line
HttpContext.Current.Response.Cache.SetNoServerCaching();
Response.ClearHeaders();
Response.Clear();
Response.Buffer = true;// Read the original file from diskFileStream *
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename[filename.Length-1]);
String ext = String.Empty;
if (strFile.IndexOf(".") > 0)
ext = (strFile.Split('.'))[1];
if (ext.ToLower() == "txt")
Response.ContentType = "text/html";
if (ext.ToLower() == "xls")
Response.ContentType = "xls/html";
if (ext.ToLower() == "pdf")
Response.ContentType = "application/pdf";
////combine the path and file name
if (File.Exists(strFile))//open the file and process it
{
FileStream sourceFile = new FileStream(strFile, FileMode.Open, FileAccess.Read);
long FileSize;
FileSize = sourceFile.Length;
Response.AddHeader("Content-Length", FileSize.ToString()); //*
byte[] getContent = new byte[(int)FileSize];
sourceFile.Read(getContent, 0, (int)FileSize);
sourceFile.Close();
Response.BinaryWrite(getContent);
}
Common.UserPageAudit(Session["User"].ToString(), "Download Templates", Session["ROLE"].ToString(), strFile + " Template downloaded");

ASP.net download file Using HttpContext.Current.Response.TransmitFile in ASP.NET

public void Downloadfile(string sFileName, string sFilePath)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STREAM";
String Header = "Attachment; Filename=" + sFileName;
HttpContext.Current.Response.AppendHeader("Content-Disposition", Header);
HttpContext.Current.Response.AppendHeader("Cache-Control", "no-cache");
System.IO.FileInfo Dfile = new System.IO.FileInfo(HttpContext.Current.Server.MapPath(sFilePath));
HttpContext.Current.Response.TransmitFile(Dfile.FullName);
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
I have a download button, the click will return the call and download the corresponding file download, but sometimes file returns is detailt.aspx file. I do not understand what is happening.
I need help. Thanks a lot
This has worked for me with out issue for a while now.
public void Downloadfile(string sFileName, string sFilePath)
{
var file = new System.IO.FileInfo(sFilePath);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + sFileName);
Response.AddHeader("Content-Length", file.Length.ToString(CultureInfo.InvariantCulture));
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
}

Server not pointing to folder

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();

Resources