my need is to display an image in a web page using string builder. i know through this snippet sb.Append("<img src=\"bla.jpg\" />"); i can display an image. Here my problem is that the image path is dynamically generated and is in a string variable.
following is the code i tried:
Dim table_builder As New StringBuilder
table_builder.Append("<table width=100%>")
Dim filePath As String = Server.MapPath("../attachments/") & fileName
// file name is the name of the file fetching from the database
table_builder.Append("<tr>")
table_builder.Append("<td>")
table_builder.Append("<img src='" + filePath + "'/>")
table_builder.Append("</td>")
table_builder.Append("</tr>")
But it does not show the image through executing the code
table_builder.Append("</table>")
attach.innerHTML = table_builder.ToString()
Server.MapPath("../attachments/") will map the physical path, that's why the image is not showing. so you can edit your code as:
table_builder.Append("<table width=100%>")
table_builder.Append("<tr>")
table_builder.Append("<td>")
table_builder.Append("<img src='" + "../attachments/" & fileName + "'/>")
table_builder.Append("</td>")
table_builder.Append("</tr>")
table_builder.Append("</table>")
attach.innerHTML = table_builder.ToString()
For your reference ,The Answer by splattne says that :
Server.MapPath specifies the relative or virtual path to map to a physical directory.
Server.MapPath(".") returns the current physical directory of the file (e.g. aspx) being executed
Server.MapPath("..") returns the parent directory
Server.MapPath("~") returns the physical path to the root of the application
Server.MapPath("/") returns the physical path to the root of the domain name (is not necessarily the same as the root of the application)
Server.MapPath will return the PHYSICAL path of the file on the server. You certainly don't want that as the file will not be accessible from the client side.
You should use relative path
like Dim filePath As String = ("/attachments/") & fileName
with that the file should be accessible from client as
http://domainname.com/attachments/filename
also be careful while using ../ in your paths. You might end up one level lower to your root path and it might not be accessible from client side
Typically ~ can be used to always start the paths from site root
Related
i want to view file which is out side of my project or drive. for that i added a key in web.config with a value of destination location.
i.e.
<add key="fileuploadView" value="file:///D:\SubhrasData\xyz\I3ms\mis\doc\" />
now i get the path in following way
Dim path As String = ConfigurationSettings.AppSettings("fileuploads")
and assign path to view the file using hyperlink.
i.e. hypSpCertificate.NavigateUrl = path & gObjDt.Rows(0)("VCH_DOC_PATH")
but it first search in its folder first(in localhost). but i want that it should directly search the give file path.
You are assigning value in key with name "fileuploadView" and getting it by "fileuploads"
Shouldn' this be like
Dim path As String = ConfigurationSettings.AppSettings("fileuploadView");
how to get the servername + foldername without containing the script name?
string filePath = Request.QueryString.Get("filepath");
string serverPath = Request.ServerVariables["SERVER_NAME"] + "/";
string fullUrl = "http://" + serverPath + filePath;
Response.Write(fullUrl);
the above code is missing folder name.
This page demonstrates how the parts of a HttpRequest break down. You could take the Request.FilePath and remove the final segment like so:
string directory = Request.FilePath.Remove(Request.FilePath.LastIndexOf('/'));
You can't quite do that, because HTTP is "blind" to what's a folder and what's a file.
You are probably familiar with URLs such as http://www.acme.com/products/view.asp but a completely valid URL could also be http://www.acme.com/products/view , so you can't differentiate a folder and a file name.
What you CAN do, provided that:
you know your application and know that it only runs files (as opposed to some routing mechanism that runs some logical piece of code, such as in MVC for example)
each file contains an extension
is to parse the path string on your own and look for file.ext pattern in the end of the path.
String DocLocation = System.AppDomain.CurrentDomain.BaseDirectory + "Files/test.pdf";
// or
String DocLocation = Url.Content("~/Files/test.pdf");
var document = new FileStream(DocLocation, FileMode.Open);
var mimeType = "application/pdf";
var fileDownloadName = "download.pdf";
return File(document, mimeType, fileDownloadName);
The first method is UnauthorizedAccessException.
The second method cant find the file.
I am trying to send a file for download. Using full desktop path seems to work.
Also, how would I display PDF in the browser instead (note, still need download option as not all are pdf)?
Try Server.MapPath("~/Files/test.pdf")
File() takes a physical path on disk.
Therefore, you can't use Url.Content, since that returns a relative URL for the browser.
Instead, you need Server.MapPath, which converts an application relative path into a full path on the local disk.
i want to upload image int the bitmap object from asp.net, the image is location under
/uploadedimages/sampleimage.jpg
whenever i use below code to load image in bitmap, i gets error saying Parameter not valid.
Bitmap b = new Bitmap("/uploadedimages/sampleimage.jpg") // this path is coming from database holded in variable
i tried to replace the slashes in path to "\" still that does not work.
can anyone tell me what could be the reason for the error and the possible resolution.
Use Server.MapPath. And it's a good practice to use the tilde char ~ to specify the web application root.
Bitmap b = new Bitmap(Server.MapPath("~/uploadedimages/sampleimage.jpg"));
if uploadedimages directory is in your App_Data folder then you should append the App_Data absolute path to your path:
Bitmap b = new Bitmap(Path.Combine(Server.MapPath("~/App_Data"), "/uploadedimages/sampleimage.jpg"));
You can use server.MapPath, pass Url string as given below.
Server.MapPath("../images/image.gif")
How can I get the virtual path for a full path in ASP classic. Note that the full path may be under a virtual directory and therefore the simplistic
virtPath = Replace(fullPath, Server.MapPath("/"), "")
method will not work.
Edit: To clarify, an example follows
Full Windows File Path (known):
\\MyServer\MyShare\Web\Site\Logs\Test.txt
My Website has a Virtual directory
called Logs that Points to \\MyServer\MyShare\Web\Site\Logs\.
Virtual Path (unknown): /Logs/Text.txt
Http path (unknown, needed):
http://Site/Logs/Test.txt
The code is in a asp page in the main app, not under any virtual directories. It is located on a separate server from the file in question.
IIS 6.0
How do I find the virtual path from the full file path?
In case anyone's interested, Anthony Jones' answer showed me the way to getting the application's relative root consistently. So if you have a site at http://example.com and a local development equivalent at http://localhost/example, you can find your root with this function:
Function ToRootedVirtual(relativePath)
Dim applicationMetaPath : applicationMetaPath = Request.ServerVariables("APPL_MD_PATH")
Dim instanceMetaPath : instanceMetaPath = Request.ServerVariables("INSTANCE_META_PATH")
Dim rootPath : rootPath = Mid(applicationMetaPath, Len(instanceMetaPath) + Len("/ROOT/"))
ToRootedVirtual = rootPath + relativePath
End Function
You can then call it like this to get the root path:
ToRootedVirtual("/")
Which will return:
/ on example.com
/example/ on localhost/example
You can also use it without the slash:
ToRootedVirtual("")
If I've understood the question.
Assumption
The full path is a path with in the current application or a child application. It is not a path limited to the parent nor a path into a sibling application. The desired path is relative to the current applications path.
Scenario 1
A path such as
"/someApp/someFolder/someSubFolder/file.ext"
should resolve it to:-
"~/someFolder/someSubFolder/file.ext"
(although the ~/ notation isn't something ASP classic understands).
Scenario 2
"/someApp/someSubApp/SomeSubFolder/file.ext"
you still want:-
"~/someFolder/someSubFolder/file.ext"
Scenario 3
The app is the root application of the site:-
"/someFolder/someSubFolder/file.ext"
would still become
"~/someFolder/someSubFolder.file.ext"
Solution
The key to solving this is:-
Dim sAppMetaPath : sAppMetaPath = Request.ServerVariables("APPL_MD_PATH")
For the above set of scenarios this will result in something like:-
"/LM/W3SVC/33230916/Root/someApp"
"/LM/W3SVC/33230916/Root/someApp/someSubApp"
"/LM/W3SVC/33230916/Root"
Also
Dim sInstanceMetaPath: sInstanceMetaPath = Request.ServerVariables("INSTANCE_META_PATH")
will in all the scenarios return
"/LM/W3SVC/33230916"
With some mathematical reduction we can arrive at the function:-
Function ToAppRelative(virtualPath)
Dim sAppMetaPath : sAppMetaPath = Request.ServerVariables("APPL_MD_PATH")
Dim sInstanceMetaPath: sInstanceMetaPath = Request.ServerVariables("INSTANCE_META_PATH")
ToAppRelative = "~/" & Mid(virtualPath, Len(sAppMetaPath) - Len(sInstanceMetaPath) - 3)
End Function
Although there may be a better way, I always did this by creating a config variable where I manually specify the root path that is not part of the virtual path. This is because you do not know if the site will be deployed as root, under a folder in root web, or in a virtual directory.
well, my answer isn't better than OrbMan's...
I have organized my app in such a way that every include is relative...
that is
instead of \myapp\lib\somefile.asp I use ..\lib\somefile.asp
in other cases I just do what Orbman said...
Here's how you solve root-relatie pathing in html via ASP so your site can be portable to different hosting directories.
This little snippet will produce the correct prefix to set your URLs:
Mid(Request.ServerVariables("APPL_MD_PATH"),Len(Request.ServerVariables("INSTANCE_META_PATH"))+6)
You can use this in LINKs, IMGs, hyperlinks, etc as follows:
<link href="<%= Mid(Request.ServerVariables("APPL_MD_PATH"),Len(Request.ServerVariables("INSTANCE_META_PATH"))+6) %>/assets/css/master.css" rel="stylesheet" type="text/css" />
so, code your paths to be root-relative (starts with a /) and then put this snippet right in front of that first slash, inside the quotes:
The server's virtual path is:
<%Response.Write "http://" & Request.ServerVariables("server_name") &
left(Request.ServerVariables("SCRIPT_NAME"),InStrRev(Request.ServerVariables("SCRIPT_NAME"),"/")) %>
</p>