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");
Related
I was trying to upload some images in my website using input type=file in my asp.net mvc project.while storing the image I used the following code and it is saving the image successfully.
var path2 = Path.Combine(Server.MapPath("~/Images"), filename.jpg);
artwork.SaveAs(path2);
After that I need to save the whole link ( http://example.com/Images/filename.jpg) into my table column.
for that i tried this
tablename.imagelink = path2 ;
then in table column I am getting like
D:\xxxxx\yyyy\example\Images\filename.jpg
then i tried to save the whole link directly
tablename.imagelink = Path.Combine(Server.MapPath("http://example.com/Images/"), filename.jpg);
at that time I am getting an error: "is not a valid virtual path error"
How to solve this. ?
Server.MapPath requires the virtual path of the web server, and you get the error because you're passing the full path like this:
Server.MapPath("http://example.com/Images/")
You need to keep using this:
Server.MapPath("~/Images")
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
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")
I have set of components that i wish to let the users download from my web application.
Now the question is where should i place the files in app_data or create a separate folder in asp.net web application as shown here or is there any other optimal solution for this ?
What i mean by components is you can take a look at this ! So what is the best way to do store the components ?
Right now what i'm doing is: i'm storing the files in a external folder outside the application more specifically in documents folder of my c drive, and i'm storing the path to a component as a data element of the table, when ever user clicks on a particular row's button (in the grid view) i'm getting the title of that particular clicked row and querying the database table for the filepath of that component title using these lines of code:
String filePath = dr1[0].ToString(); //GETS THE FILEPATH FROM DATABASE
HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STREAM";
String disHeader = "Attachment; Filename=\"" + filePath + "\"";
HttpContext.Current.Response.AppendHeader("Content-Disposition", disHeader);
System.IO.FileInfo fileToDownload = new System.IO.FileInfo(filePath);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.WriteFile(fileToDownload.FullName);
Am i doing it properly ? Is there a better/optimal way to do it ?
A user simply needs read access to download a file, so you can simply create a directory claled "Downloads" and place them in there.
You can ensure that people can't "browse" that directory by disabling Directory Browsing and not placing any default docs in there (index.html, default.aspx for example)
What you are currently doing looks like a fairly standard way for providing downloads off your site.
I can't think of something more "optimal".
My project contains web.config file and an external appSettings file. I am making a WebConfig Editor that has options to Read AppSettings key from web.config and external appSetting file to display them on webPage.
Also, I am allowing user to delete any key by clicking on Remove button.
Moreover, user can also update any key's value by clicking on update button.Or
he can also insert new key by clicking on Add New Key button.
The key issue I am facing is that whenfever I try to add a new key , it gets inserted into
web.config file as expected , but at the same time it adds all the keys present in external appSettings file into web.config ( which is abrupt behavior).
How to stop this migration of keys from external appSettings file to web.config on any key's update / delete/ add function?
For reading, put external file in Config folder under root and then use this code to read key/values based on key name it read from web.config or external file.
// get from web.config
String myKey = ConfigurationManager.AppSettings.Get("Key1");
String str += "AppSetting value from web.config:" + myKey;
// get from external AppSetting file
myKey = ConfigurationManager.AppSettings.Get("Key2");
String str2 += "AppSetting value from external AppSetting file:" + myKey;
where Key1 is in web.config and Key2 in external config file
also
to find al key values use foreach loop
foreach (string key in ConfigurationManager.AppSettings)
{
string value = ConfigurationManager.AppSettings[key];
Console.WriteLine("Key: {0}, Value: {1}", key, value);
}
While reading the keys add a unique signature with the keys of web.config file and external app settings file.
on web only show keys not the signature, and when u add keys add the same signature ( if adding for web.config then web.config's Signature ) and when writing to the web.config apply the check for signature if the signature for the key is of external app settings file then ignore the key otherwise write the key.
Its the Simple solution, well if u have any query do ask