Link to home
Start Free TrialLog in
Avatar of CodeJunky
CodeJunkyFlag for United States of America

asked on

ASP Web Form to open files

Hi all, I'm very new to this; but have been coding in VB.NET for decades.  I want to place hyperlinks on a form and when clicked, grab the tag value and open the file, such as a pdf, word, exec, etc..  In just VB I would add a hyperlink, this is client/server of course, and in the click event grab that tag value, being the file name and location, and pass that onto a function to open the file.

I don't even know if this is best practice; but it is what I am familiar with.

Avatar of HainKurt
HainKurt
Flag of Canada image

<a href="openFile.asp?FID=782364">File1</a>
<a href="openFile.asp?FID=873645">File2</a>
<a href="openFile.asp?FID=762455">File3</a>

openFile.asp

get the FID Request("FID")
do some lookup on db to find path and check session login if required
then open the file as binary
write to response

check this, it is for .Net but ASP is almost identical

Use ASP.NET and Visual C# .NET to write binary files to the browser

https://docs.microsoft.com/en-us/troubleshoot/aspnet/write-binary-files-browser-csharp
Avatar of CodeJunky

ASKER

Thanks HainKurt,
I don't understand this.  I don't necessarily need to open file inside the browser.  Everyone has a pdf viewer and microsoft office, so the system association should automatically be able to open any file.

Where do I get the FID from.
Do I place the full path in the File1, File2, etc....

I was expecting a click-event; but I don't think the Hyperlink has one like a button does.
you dont need click event
your page will list the files with FID, a unique identifier that helps you find the file info from somewhere, probably db
and after getting file info, you will read the file, and put the content in response
set the meta tags and send to browser/client
then browser will get this and based on meta tag, it will open associated app and show the document retrieved...
Avatar of Scott Fell
You said click event, are you trying to do this using client side javascript?

In that case, it could look like
HTML
<a class="doc" id="abc123" data-document="somefile.docx">Some File</a>

Open in new window

JAVASCRIPT/JQUERY
$( document ).ready(function() { 
      $('.doc').on('click',function(){
            //grab file from server directly if on the public www
            // or ajax request to call up file if hidden from the public
      });
});

Open in new window

As an aside, if your documents are at least semi-private, it is good practice to keep them out of the public www. If the document lives in the public like www.mysite.com/files/document1.docx then anybody with that URL can grab the file. You can instead store the files outside of your public folder and user your vb to call up the file on the server and send it to the browser (to open in the browser or force download).
Just to clarify, are you trying to do this by a website? If so using classic asp with vbscript or ASP.NET with vb?  Or is this a VB.NET or visual basic program you are working on?
This is an ASP.net application using winforms.
I'm currently working on this project on my desktop environment and would move it to an IIS server.  I would want to place the files in a folder, on the server eventually, and source them from that location, whatever that location is going, or should, be.  At this point I'm not using a database.
so this is internal application, not open to internet...

if so, something will just work fine...

<a target="_blank" href="file:///Z:/documents/File1.pdf">File1</a>
<a target="_blank" href="file:///Z:/documents/File2.docx">File2</a>
<a target="_blank" href="file:///Z:/documents/File1.xlsx">File3</a>

no OnClick event or any other code is required...
That would be perfect but it didn't work.  nothing happens when I click on the link.
Does it matter if the file has spaces in it?  The file is also on my computer.
I just realized I'm using Edge Chromium. Like Chrome, it does not support direct file access. Is this correct?
Just tried with IE and it still does nothing.
it does not matter
copy paste one of the file code here, so I can check what's wrong...

<a target="_blank" href="file:///Z:/documents/File1.pdf">File1</a>

That will not work on a iis web server
 <p>
                <a target="_blank" href="file:///F:/DEV/ARDC_ASP/DocumentStores/Financial/Commit Payments Instructions.pdf">Commit Payments Instructions</a> 
            </p>

Open in new window

I used

<p>
  <a target="_blank" href="file:///C:/Users/HainKurt/Documents/EE/Commit Payments Instructions.pdf">Commit Payments Instructions</a> 
</p>

Open in new window

User generated image
then opens it

User generated image
are you sure path is correct?
if browser does not allow this, then
on web server, you need to create a virtual folder and point to the path where files are...

for example, your page is here

c:\www\mySite

which is set as root for default web site on server
here, you define a Documents folder and point it to F:/DEV/ARDC_ASP/DocumentStores/

then your page will be

<p>
  <a target="_blank" href="/Documents/Financial/Commit Payments Instructions.pdf">Commit Payments Instructions</a> 
</p>

Open in new window

then it will work...
This is what my address bar reads when I click on it.  Tried moving it to another drive too.
User generated image
I'll give that a try in a little bit.  I'm tired right now.  Thanks so very much for you help and patience, I very much appreciate it.
ASKER CERTIFIED SOLUTION
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The workflow being proposed is wrong and should not work on an iis web server
it works everywhere as long as it is in intranet not open to public/internet...
if it is internet, I already suggested what to do...
here
https://www.experts-exchange.com/questions/29201954/ASP-Web-Form-to-open-files.html#a43204061 
and after that post...

Thanks to both of you.  I will review all this and get back to you.  This project is entirely internal and not in the public domain in any way.