如何将任何文件下载到任何设备并查看? [英] How to download any file to any device and view?

查看:89
本文介绍了如何将任何文件下载到任何设备并查看?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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).





< b>我尝试了什么:





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);
    }

推荐答案

服务器代码访问服务器文档 - 客户端代码根本无法访问客户端文件,也没有直接访问访问服务器文件 - 您必须将文件下载到客户端才能查看内容。然后你就有了这台计算机有一个可以读取这种文件类型的程序吗?的乐趣。 ...



服务器代码不能直接(或间接)强制客户端打开任何文件 - 如果你考虑一下,这将是一个安全点的噩梦视图:当你访问某个网站时打开的勒索软件,任何人?



最好的办法是将服务器上的文档转换为HTML(并根据文档类型) ,有转换器会这样做)并将其作为网页呈现给用户 - 你知道他们可以查看。
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.


引用:

我所做的只是将文件的完整路径从Api返回到AngularJS控制器并且我正在使用条件



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



在文件路径所在的消息部分。这将在Web浏览器的新选项卡中打开该文件。谢谢大家的支持 。学到了很多东西。

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.


这篇关于如何将任何文件下载到任何设备并查看?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆