View files with Hyperlink ASP.NET - c#

I have a directory containing files (images , pdf ..etc).. then I've got all the directory files with c# code and viewed them as a hyperlink for each file to be able to view each file in the browser.
when I click on the pdf file link for example , it doesn't view the file .. but when I copy the link address of the pdf file , then paste and view it in a separated window . the pdf file open normally.
when I view the source code of the page I find that the link for the pdf file as it is in my local machine directory not the server one.
Please help me with that..
Thanks.

Its a security issue. In console, you must be seeing the error "Not allowed to load local resource". On clicking the anchor tag, which contains the absolute path to local file, the file has to be served by the IIS server(as the page is web page, which is loaded from asp.net application), which is not allowed.
In a web application, if you provide local file path to a file in your webpages, you are making the assumption that the file exists on the clients computer or device and not on your server.
So, create a folder inside your applications root folder and just give relative path to those files in anchor tag that includes hostname like
http://myserver.com/aspnet.pdf

Related

Project folders data can not be uploaded when publish on server in asp.net webforms

I have a folder called GalleryImg, the user can upload images dynamically, which is stored in this folder. But when I publish the project on real server then this folder can not be uploaded. I have attached the image of this folder below -
As you can see this folder contains many images but it does not shows any images here. please help me about this issue.
If folder is empty, it will not be published. So you can put a placeholder.txt file with build action set to "Content" inside this folder to force publish.

System IO exception even though file is added in Visual studio C#

I have a project into which I added a JSON file, but when I try to read the file in code I get this error.
Could not find file 'C:\\Program Files (x86)\\IIS Express\\client_secret.json'.
"ExceptionType": "System.IO.FileNotFoundException"
I have the file added in like this.
I can access it when I copy the full path and then read it.
its Asp.NEt or WEBAPI, i.e. its Web application you can access path by using Server.MapPath method.
Example : Server.MapPath("~/script/data.txt")//this locate file in your script folder on sever
for the folder under than website you need to do like this
Server.MapPath(~/client._secret.json) //here ~ sign means relative path from root
Server.MapPath method gives you physical path of your file on server machine.

Access a jpg in an Image folder of a project

I am working on a program that generates a PDF using a third party .dll. I am trying to add a header image to the PDF by sending in the string path of the jpg (Logo.jpg) in the project that is in a folder I created (Images), but it is not working using code like this..
PDFPage.AddHeaderImage("String path of the jpg in the project")
PDFPage.AddHeaderImage("~/Images/Logo.jpg))
There is a "could not find file" exception.
It works fine when I point to a file on my computer like this...
PDFpage.AddHeaderImage("C:/Source Code/Source/images/Logo.jpg");
But, I do not want to point to a file on my computer. I want to point to a the file in the Images folder in my project where I put Logo.jpg. I also put the image in Resources, but do not know what the string would be to access it. Either way would be fine.
the .AddHeaderImage is expecting a string path.
If you set Build Action = Content and Copy To Output Directory = Copy Always on the properties of the image file in Solution Explorer in Visual Studio, then the image file will be output to your bin folder along with the compiled application. You can then use a path relevant to the executable.
PDFPage.AddHeaderImage("Images/Logo.jpg));

Source file issue with File Upload Control

I have a problem with a web form project.
The project is set to use the local IIS web server to run the application.
In the form, I have a file upload control defined in the aspx markup page.
However, when I try to upload a file, the program always thinks that the file is located in the IIS Express folder, whatever the location of the file actually is.
I have tried to change the settings to make it use the visual studio development server but I hit the same issue. The program then always thought that the file to upload was in the development server folder.
How can I fix that?
Thank you.

Solution file opens up precompiled code in Inetpub

I have a folder that contains ASPX code and its codebehind (C#) as well as a solution file. The compiled version of the application resides in the inetpub directory.
Whenever I open the solution in Visual Studio, VS warns me that it is attempting to open a precompiled website and asks me if I want to continue. If I click NO it does nothing, but If I click yes it shows me the ASPX files in the inetpub directory.
How do I make the solution work with that code in the directory that it is in (ie with the aspx and codebehind)?
Thanks
Sounds like this solution was created by going to File->Open->Web Site and then saved afterward. In orther words, this is not the original solution file.
I suggest simply creating a new solution. Add the exisitng .csproj if appropriate (ie, you have a Web Application Project), or the existing web site from the directory your code is in.
Before you open your solution try opening your .sln file in Notepad or some other editor and double check that your web site path is right.
Debug.AspNetCompiler.VirtualPath = "/Url..."
Debug.AspNetCompiler.PhysicalPath = "Physical Path from the solution file\"
Debug.AspNetCompiler.TargetPath = "PrecompiledWeb\this might be the same as your physical path?\"

Categories

Resources