单击按钮如何查看服务器上的文件? [英] How to view a file located on server on click of a button?

查看:187
本文介绍了单击按钮如何查看服务器上的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

点击按钮(按钮位于192.xxx.x.xxx \ Profile \ Details页面中)时,我会将一个ID传递给正在调用API的angularJS控制器文件

I'm passing an Id on click of a button(button is in 192.xxx.x.xxx\Profile\Details page) to angularJS controller file where it is calling an API

$scope.docView = function () {     
    Method.getbyId("api call",docId).then(function(response) {
            if (response.data.message != null)
                window.open('//'+response.data.message);
            else
                alert("File Not Found !");
        }).catch(function (data) {
            console.log("Unknown Error");
        });
    }
}

API是:

    [Route("api call")]
    [HttpGet]
    public IHttpActionResult ViewDocument (Guid? docId)
    {
          /*background work*/            

                response.Message = filePath;
            }                
        }
        catch (Exception ex)
        {
            Utils.Write(ex);
        }

        return Ok(response);
    }

在response.data.message中,服务器上文件(192.xxx.x.xxx \ folder \ filename)的路径即将到来,这将通过window.open()打开该文件.但是在URL中,文件夹的整个路径都是可见的,这将成为安全漏洞.因此,我想在同一控制器中在新视图中显示文件",这将在新选项卡中打开文件.网址应类似于(192.xxx.x.xxx \ Profile \ Details?docid = xxxxxxxxxxxxxxxxx).

In response.data.message, the path of the file(192.xxx.x.xxx\folder\filename) on the server is coming which will open the file via window.open(). But in URL, whole path of the folder is visible which will become security breach. Hence I want to 'display the file in a new View' in the same controller which will open the file in a new tab. The url should be like (192.xxx.x.xxx\Profile\Details? docid=xxxxxxxxxxxxxxxxx ).

推荐答案

您真正要寻找的东西,可以在Google上进行快速搜索找到:从ASP.NET Web API方法下载文件".

What you are actually looking for, you can find with a quick search on Google: "Download file from an ASP.NET Web API method".

例如此处此处,您将找到示例可以实现您要寻找的东西.

For example here and here you'll find examples which implement what you are looking for.

HttpResponseMessage response = new HttpResponseMessage();
response.StatusCode = HttpStatusCode.OK;
response.Content = new StreamContent(result);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue(System.Net.Mime.DispositionTypeNames.Inline)
{
    FileName = "foo.pdf"
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");

源自线程

您需要根据实际文件类型更改Content-Type.另外,如果实际上在浏览器中可以查看该文件,则取决于浏览器是否能够做到这一点.

You would need to change the Content-Type according to the actual file type. Also if the file is actually viewable inside the Browser depends on wether the Browser is able to do so.

这篇关于单击按钮如何查看服务器上的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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