How to delete file from relative path in asp.net - c#

My Debugging screenshot
I am trying to delete a file in asp.net web 4.7.1. I'm using FileInfo and checking if file exists using a relative path the points to the Uploads folder and image full name and extension. I have saving the image path in my database and I know if I use full path I will delete the file. How can I delete using the saved path?
I am thinking of modifying my database for each Image to store full path and relative path. I will use the full path for deleting file only. How can I delete without having to create a second database field for storing a full path?

One of the best ways to solve such problem (So that application wouldn't face any challenge of path access or read\write access on windows drive level).
Create virtual director and use that virtual directory to save relative path. Set up all access on virtual directory which is one time task.
Then you can use that virtual directory relative path in your code instead of full path.

Related

TelegramApi C# GetFile

I don't know how to specify the path to the directory where I need to save the file.
Please tell me how can I save the file to the desired directory received from the user and also create a separate folder for this user in the directory?

How can I use Server.MapPath to upload a file onto a server which is mapped on my computer

So far I know how to upload a file to a folder within my solution using the code below.
string root = HttpContext.Current.Server.MapPath("~/upload");
How can I save the file to a different location that is not within a solution i.e to a server location that is mapped to my pc.
string root = HttpContext.Current.Server.MapPath("/Z:/UploadFolder"); I have tried this but its not saving to the server so where I am going wrong?
You should use MapPath when you have a relative path and want to use the path to your project. for another path you don't need MapPath. just use it like this:
string root ="Z:\\UploadFolder";
You can map a virtual directory in IIS to the location you want to save to. For example, map a virtual directory of UploadFolder to z:\uploadfolder. Then, this will work:
string root = HttpContext.Current.Server.MapPath("~/upload");
Make sure you set permissions appropriately.
Your logic seems to be confusing. Use Server.MapPath - Returns the physical file path that corresponds to the specified virtual path.
But you are passing Physical Location to Server.MapPath in the second statement, which fails the whole purpose of Server.MapPath.
string root = HttpContext.Current.Server.MapPath("/Z:/UploadFolder"); **INCORRECT**
Ideally, you would need to create a virtual directory mapping to "/Z:/UploadFolder" and name it as "upload".
string root = HttpContext.Current.Server.MapPath("~/upload");
NB: You would need to pass explicit credentials to access the network share from ASP.NET. The suggested approach is to use Identity impersonation, once it is done retry with the same logic.
<configuration>
<system.web>
<identity impersonate="true" />
</system.web>
</configuration>

Display pdf file from absolute path

I am using this code for creating pdf viewer in my application
https://amitpatriwala.wordpress.com/2009/08/28/pdf-viewer-in-asp-net/
it works fine when I give it a path for a file inside my application folders, ex: displaypdf1.FilePath= #"~/MyFolder/" + Hello.pdf;
but now I want to give this displaypdf1.FilePath an absolute path to read the pdf file which is not in my application folders, I tried but it didn't work!
A web page cannot access items that are outside of the website. If you want the web page to reference files located in D:\PDFs, for example, you need to create a virtual directory in your website that points to "D:\PDFs". Then the web pages can access them by ~/PDFs/Hello.pdf.
You'll also need to ensure that the website has appropriate permissions to access the directory.

Options for storing file path of the component in database

I have set of files (which are essentially ".exe" files) that I allow the users to download from my website. To have a clearer picture have a look at the this screenshot (it is just a academic project). Now I have administrator privilege in which I can upload a new software file to a folder (componentsFolder) to the root of my website and I also add the filepath to the database table at the same time.
I'm using the following code to do that:
string componentRelativeFilePath = #"/ComponentsFolder/" + ComponentName;
I'm storing the filepath in the following format in the database file:
/ComponentsFolder/FileName.exe
What is the difference between storing the files in the following formats?
/ComponentsFolder/FileName.exe
\ComponentsFolder\FileName.exe
~/ComponentsFolder/FileName.exe
~\ComponentsFolder\FileName.exe
I'm using server.mappath to retrieve the file from the root folder.
I want to know the difference (in this context) between these formats and which one is the standard/appropriate/technically correct format to store the relative paths in database table.
In terms of Asp.Net lets suppose you set your image path as "/Image/pic1.jpeg" so the image would be searched in Image folder located in website root and in that folder pic1.jpeg is searched. If your set you image source to "~/Image/pic1.jpeg" in that case as well the image file is read from the Image folder that is located directly under the root of the Web application, regardless of where in the Web site the page is located. But '~/' this could only be used with server controls.
If path is "../Image/pic1.jpeg", in that case Image folder is searched in the current webpage's folder.
As per my opinion storing path in "~/Image/" format is a better choice in terms of Asp.Net.
Hope I answer your question.

Convert physical path to web path asp.net

I need to display an Image in web page. But the Image doesn't exist in the Web directory. If the image is under web directory I know that just "../Images/TN/my.jpg" will work. But the image is available in "D:\Images\TN\my.jpg" and My web site is deployed in "C:\apps\mywebsite".
How do I convert the "D:\Images\TN\my.jpg" path to a relative path so that the Image will be visible in web page?
You cant show images if they are not in a virtual directory.
Try holding then in your application folder itself or if its not feasible then make your Folder holding the image as a virtual directory in the IIS and mount it in your application folder.
Here's what you need to do (way i prefer),
Open IIS manager.
In Connections under Sites Select your Website and RightClick on it.
Select `Add Virtual Directory`... from the Context menu.
In The dialogue box that opens Enter the Alias name of your choice like **`"GlobalImages"`**
Browse the physical path of the Folder you want to and Select it and Click OK.
You will have it available for relative url in that website. like
www.example.com/GlobalImages
When you set that in IIS, it is automatically added to your Solution's Project.
Here is the MSDN link if you want..
http://msdn.microsoft.com/en-us/library/ms751432.aspx
Easiest thing to do is probably set up a handler and use the generic image name. While local, point to the D:. While on the server, point to the web path. (At least so long as you're not keeping them within the virtual directory that is).
TO fetch this point, we always write the absolute directory for images or other resource.But if the resource in the web server, i use the relative directory.

Categories

Resources