Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The below code is an API call in which I'm passing a documentID and I'm trying to open a document. Due to the fact that this whole process is running on a server I'm not able to view the file on any other device(be it another desktop or mobile device) although the file will open up in server machine but wont open locally. Can anyone please guide me through as to where I'm going wrong ? (sorry for the code, I know it might not be perfect as I'm new to web dev. still learning).


What I have tried:

{
        int i = 1;
        string key = ConfigurationManager.AppSettings["PhysicalDocumentPath"]; // some address like "xxx.xxx.xxx.xxx\folder\documents...."
        string key2 = ConfigurationManager.AppSettings["PhysicalDocumentPath2"]; // "C:\{somefolder}\{somefolder}...."
        JAppDoc value = new JAppDoc();
        var response = new Response();           
        try
        {
            if (!Directory.Exists(key2))
            {
                Directory.CreateDirectory(key2);
            }

            IAppDataService appDataService = new AppDataService();
            response = appDataService.GetDoc(docId, value);               

            var fileName = value.ApplicantId + "_" + value.DocumentName;
            var savefileName = fileName;
            var fileSavePath = Path.Combine(key, fileName);
            var prevPath = fileSavePath;
            var nextPath = fileSavePath;
            var tmp = fileName.Split('.');
            var tmp1 = tmp[0];                
            while (File.Exists(nextPath))
            {
                tmp = fileName.Split('.');                    
                fileName = tmp1 + i.ToString();
                fileName = fileName + "." + tmp[1];
                savefileName = fileName;                    
                nextPath = Path.Combine(key, savefileName);
                if (File.Exists(nextPath))
                {
                    prevPath = nextPath;
                }
                i++;
            }

            try
            {
                tmp = prevPath.Split(new[] { "Docs\\" }, StringSplitOptions.None);
                var serverpath = key + tmp[1];
                var localpath = key2+ tmp[1];
                if (File.Exists(localpath))
                {
                    Process.Start(localpath);
                }
                else
                {
                    System.IO.File.Copy(serverpath, localpath);
                    Process.Start(localpath);
                }
            }
           catch(Exception e)
            {
                Utils.Write(e);
                response.Message = "File not found !";                    
            }
        }
        catch (Exception ex)
        {
            Utils.Write(ex);
        }

        return Ok(response);
    }
Posted
Updated 27-Feb-18 3:37am
Comments
F-ES Sitecore 26-Feb-18 6:34am    
Your code is running on the server so can only access documents on the server. You can't access the client's file system if that is what you're looking to do. You can access a network share like \\somecomputer\folder\file.doc but you will need to change the account your code runs under in IIS to an account that has the required network access.
Member 13512111 26-Feb-18 6:38am    
I just want to view the document on the client side, be it a mobile or ipad or desktop whatever.
sorry i din't get the access thing . can you please tell more about it ?

Server code accesses server documents - client code can't access client files at all, and has no direct access to server files - you have to download the file to the client in order for them to view the content. And then you have the fun of "does this computer have a program that can read this file type?" ...

Sever code cannot directly (or indirectly) force the client to open any file - if you think about it, that would be a nightmare from a security point of view: ransomware that opens when you visit a site, anyone?

Your best bet is to convert the document on the server to HTML (and depending on the document type, there are converters that will do that) and present it to the user as a web page - which you know they can view.
 
Share this answer
 
Comments
Member 13512111 26-Feb-18 6:56am    
First of all thank you for the previous suggestion. Btw can't I do something like return the file from server to client side(from api to angularjs controller of a webpage) to view it ? or download the file on local and then view ? Or maybe open it up in a new tab and just view it ? basic motto is to view the file.
OriginalGriff 26-Feb-18 7:11am    
You can download the file, but then it's up to the user what he does with it - you cannot force his browser to open, view, save, or even not delete it! And even if you do, there is no guarantee that whatever device he is viewing your site from has an application which can open the document. About the only format you can guarantee he can view is ... HTML (although PDF comes close, but not all browsers will automatically view it).
Member 13512111 26-Feb-18 7:32am    
Ok so can you guide how to I convert the files ? For now there is no restriction on the type of file a user can view. It can be anything like text,image etc. Or just how to download the file ?
OriginalGriff 26-Feb-18 7:44am    
There is no way to "convert anything" - you need a (probably specific) reader for each source type that can produce HTML.

What exactly are you trying to do here? There may be an easier way to solve the whole problem that going this route at all.
Member 13512111 26-Feb-18 7:47am    
I just want to open a file on click of a button. That button is being called from angularJS controller(ajax call) which is making an API call(IHTTPActionResult). This api is from where the file has to open/download then open whatever.
Quote:
Well what I have done is just returning back the full path of the file from Api to AngularJS controller and I'm using condition

if (response.data.message != null) window.open('//'+response.data.message); else alert("File Not Found !");

in the message part the file path is there. This opens the file in a new tab in the web browser. Thank you everyone for your support . learnt a lot.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900