Download failed in anchor tag - c#

I am using acnchor tag to download file on click of it but on click the file fails to download and shows Failed-Network error in in chrome browser.
I changed my anchor tag on different ways but non of them working
<td>#file.FileName</td>
<td><a href="#file.FilePath" download>#file.FileName</a></td>
<td><a href="#file.FilePath" download=#file.FileName>#file.FileName</a></td>
What is problem in it?

This is a client-server issue. That path exists on the server, but the download is initiated by the client. The client doesn't have access to the server's filesystem, so it fails. If you need stuff in that directory available for download, create a virtual directory under your site that points to that path, then link the user to the file under the virtual directory, e.g.:
Click me!

Related

No webpage was found for the web address when trying to download file

I have the following structure:
AppName
Content
img
file.png
I can navigate directly to mysite.com/Content/img/file.png without any issues.
However when I add test.txt to img and set to Copy Always and type Content (same as file.png), I get No webpage was found for the web address when visiting the link, when I want to be able to enable folks to download the publicly accessible file (no auth needed), just like the image. Why isn't this working?
I can see test.txt in the bin folder of the app. Does something else need to be configured to enable direct file downloads?
Make sure your IIS site settings or web.config have mime type associated with .txt extension.
https://www.iis.net/configreference/system.webserver/staticcontent/mimemap

FTP upload in .net — not getting correct file path in some browsers

I'm building an application which involves writing some fields to a database, along with uploading some files from the end user to an FTP site. The file upload works fine... in IE. In Firefox and Chrome, I get an error that it can't find the file (running it in localhost at this point, haven't moved it to a dev or production environment yet).
I have tried getting the file via:
Server.MapPath(FileUpload1.PostedFile.Filename)
... which points to the folder the application is residing in.
And also:
Path.GetFullPath(FileUpload1.PostedFile.Filename)
... which points to c://Programs (x86)/... ...
I can get a file to upload properly if I get it from either folder, but nothing from anywhere else.
Any ideas on how to make this point to the right place? Or, will it actually work properly once it resides in a server environment?
Thanks in advance!
FileUpload.PostedFile.Filename works differently in each browser. in Firefox and Chrome it won't include the full path - just the file name. It depends on your customer's browser.
FileUpload.PostedFile.FileName
This actually gives you path of the uploaded file.
But in all the newer browsers (FF 3.6 series, Chrome, IE7+) this feature has been disabled due to security reasons. Any website should not need path of a file stored in client's systems because that gives the directory structure and may expose other important things to website owner.
So in your case, the above code returned only the file name.
you can check this link, it may help you Fileupload control - fullpath issue

How to load an image from the server in a web application in C#

I actually have a C# winform application which load images from my computer C://images/... with the Image object and the function Fromfile.
Image.FromFile(Path);
but in my web application (ASP)
<asp:Image ID="viewPhoto" runat="server" Width="550px" Height="400px"/>
I use the attribute ImageURL.
viewPhoto.ImageURL = Path
But the problem is that it doesn't find the correct path because with this way. The path will be http://localhost:3656/C://images....
I would like to load an image directly from my server to have the correct path for both of my applications.(web ASP and winform)
Image.FromFile(/images/myimage.jpg)
This actually doesn't work because the program doesn't find any photo in this path.
First of all i think that images you are trying to show are not in web application folder / virtual directory. Move images folder to your web application folder and then use:
Page.ResolveClientUrl("images/test.jpg");
or for server side:
Server.MapPath("images/test.jpg");
If you dont want to move images to your web folder then your only choice is to write HttpHandler which will read images from C:\images folder and transmit it to the client. This will also require some specific permissions for your web app IIS user to access some folder outside the web app scope.
You can see the sample of HttpHandler here: Thumbnailer HTTP Handler
ASP.Net image simply renders the ImageURL to the client browser, this will then try to load that image from the location specified, this typically needs to be a resource available via your website.
Try moving your images inside your web root then you can access them like :
viewPhoto.ImageUrl = "~/images/yourimage.jpg";
As im sure you know your <asp:Image> element is actually creating an html <img> element that will instruct the browser to request the image from the browser.
Image.FromFile will give you an image object on the server side but it won't be much use for your clients browser.
As #HABJAN said, you need to map the path from your local file to a relative web URI and to do that you can use Server.MapPath. However your images will need to be inside your asp.net project folder which is asccessible by the web server in order for it to serve the file.
If you really want to share the paths in some sort of shared constant assembly I suggest you make your windows application work with relative paths and duplicate the images folder structure for both the windows app and the web app.
I found the quickest way to resolve this was to assign the image path as a css attribute. The only downside of this is that if you are looking to display a large amount of images in a repeater you would not want to use this, but if you have one or two images you can use this.
<style type="text/css">
#errorImage{
background: url(/path/path/images/image.jpg);
background-size: contain;
height: 100px;
width: 100px;
}
</style>
<div id="errorImage"><div>
Using the path you are using now (http://localhost:3656/C://images....), the application will look for a /C://images... folder in your app directory on the web server. I believe you should create an images directory in your application folder and server the images from there so that you could reference the images using a relative path.

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.

How to open a file stored in a server from aspx page using UNC share path?

I am listing the files stored in a remote server in the aspx page grid view with the file name linked (href) to the UNC path of the file.
when the user clicks the file name link, it should open the file from the server to allow the user read the file.
I am using impersonation (Using mirror logon method) for file upload and delete in the same page which works fine.
When I try to click the link to view the file it throws an error message box saying access denied.
While upoading / deleting file from my application, it uses the impersonated account I specified in web.config.
But it uses default ASP.NET (SERVER\IUSR_SERVER) account when trying to access via the link.
I cant give read access to all in my file server share.
How can I make the application use the impersonated account while trying to access the file for reading through the link I am showing for the files?
Or please suggest me an better alternative to do the same.
Thanks In advance.
When you generate the links in the Web page, do not point directly to the UNC, but to a page of your app, with a parameter to identify the file. In this page, use the same impersonation method to open the file and send its content back to the Web browser.

Categories

Resources