【问题标题】:Sending a file to client gives file not found exception when file is there当文件存在时,向客户端发送文件会导致找不到文件异常
【发布时间】:2019-04-04 11:14:27
【问题描述】:

我有一个为 vue 网站提供服务的 c# 服务器。在服务器上,我有一个希望传输给客户端的文件。目前服务器托管在本地,但稍后将转移到 azure。

为了获取文件,我有以下 c# 函数:

[Microsoft.AspNetCore.Mvc.Route("api/BIM/GetModifiedRevitFile")]    
    public ActionResult GetModifiedRevitFile(string json)
    {
        //string tmppath = Path.GetTempPath();
        string tmppath = "A:\\code\\Workspaces";
         var FileVirtualPath = tmppath + "\\result.rvt";
        var fileUri = new Uri(FileVirtualPath);
        var referenceUri = new Uri(System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location));
        FileVirtualPath = referenceUri.MakeRelativeUri(fileUri).ToString();
        generateFile(FileVirtualPath) //stores the file in FileVirtualPath
        var res= File(FileVirtualPath, "text/plain", Path.GetFileName(FileVirtualPath));
        return res;
    }

为了请求文件,我在 vue 中有以下 javascript 函数:

GetFileFromServer: function() {
                var self = this;

                $.ajax({
                    type: "GET",
                    timeout: 12000000,
                    url: 'api/BIM/GetModifiedRevitFile',
                    contentType: "application/json",
                    dataType: "text",
                    success: function (data) {
                        self.AskToStoreFile(data, self);
                    },
                    error: function (data) {

                        alert("failed to load data" + JSON.stringify(data));
                    }
                });
            }

但是,当我调用此函数时,我收到以下错误(来自警报):

failed to load data{"readyState":4,"responseText":"<!DOCTYPE html>\
\<html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\">\
\    <head>\
\        <meta charset=\"utf-8\" />\
\        <title>Internal Server Error</title>\
\        <style>\
\            body {\
\    font-family: 'Segoe UI', Tahoma, Arial, Helvetica, sans-serif;\
\    font-size: .813em;\
\    color: #222;\
\    background-color: #fff;\
\}\
\\
\h1, h2, h3, h4, h5 {\
\    /*font-family: 'Segoe UI',Tahoma,Arial,Helvetica,sans-serif;*/\
\    font-weight: 100;\
\}\
\\
\h1 {\
\    color: #44525e;\
\    margin: 15px 0 15px 0;\
\}\
\\
\h2 {\
\    margin: 10px 5px 0 0;\
\}\
\\
\h3 {\
\    color: #363636;\
\    margin: 5px 5px 0 0;\
\}\
\\
\code {\
\    font-family: Consolas, \"Courier New\", courier, monospace;\
\}\
\\
\body .titleerror {\
\    padding: 3px 3px 6px 3px;\
\    display: block;\
\    font-size: 1.5em;\
\    font-weight: 100;\
\}\
\\
\body .location {\
\    margin: 3px 0 10px 30px;\
\}\
\\
\#header {\
\    font-size: 18px;\
\    padding: 15px 0;\
\    border-top: 1px #ddd solid;\
\    border-bottom: 1px #ddd solid;\
\    margin-bottom: 0;\
\}\
\\
\    #header li {\
\        display: inline;\
\        margin: 5px;\
\        padding: 5px;\
\        color: #a0a0a0;\
\        cursor: pointer;\
\    }\
\\
\    #header .selected {\
\        background: #44c5f2;\
\        color: #fff;\
\    }\
\\
\#stackpage ul {\
\    list-style: none;\
\    padding-left: 0;\
\    margin: 0;\
\    /*border-bottom: 1px #ddd solid;*/\
\}\
\\
\#stackpage .details {\
\    font-size: 1.2em;\
\    padding: 3px;\
\    color: #000;\
\}\
\\
\#stackpage .stackerror {\
\    padding: 5px;\
\    border-bottom: 1px #ddd solid;\
\}\
\\
\\
\#stackpage .frame {\
\    padding: 0;\
\    margin: 0 0 0 30px;\
\}\
\\
\    #stackpage .frame h3 {\
\        padding: 2px;\
\        margin: 0;\
\    }\
\\
\#stackpage .source {\
\    padding: 0 0 0 30px;\
\}\
\\
\    #stackpage .source ol li {\
\        font-family: Consolas, \"Courier New\", courier, monospace;\
\        white-space: pre;\
\        background-color: #fbfbfb;\
\    }\
\\
\#stackpage .frame .source .highlight li span {\
\    color: #FF0000;\
\}\
\\
\#stackpage .source ol.collapsible li {\
\    color: #888;\
\}\
\\
\    #stackpage .source ol.collapsible li span {\
\        color: #606060;\
\    }\
\\
\.page table {\
\    border-collapse: separate;\
\    border-spacing: 0;\
\    margin: 0 0 20px;\
\}\
\\
\.page th {\
\    vertical-align: bottom;\
\    padding: 10px 5px 5px 5px;\
\    font-weight: 400;\
\    color: #a0a0a0;\
\    text-align: left;\
\}\
\\
\.page td {\
\    padding: 3px 10px;\
\}\
\\
\.page th, .page td {\
\    border-right: 1px #ddd solid;\
\    border-bottom: 1px #ddd solid;\
\    border-left: 1px transparent solid;\
\    border-top: 1px transparent solid;\
\    box-sizing: border-box;\
\}\
\\
\    .page th:last-child, .page td:last-child {\
\        border-right: 1px transparent solid;\
\    }\
\\
\.page .length {\
\    text-align: right;\
\}\
\\
\a {\
\    color: #1ba1e2;\
\    text-decoration: none;\
\}\
\\
\    a:hover {\
\        color: #13709e;\
\        text-decoration: underline;\
\    }\
\\
\.showRawException {\
\    cursor: pointer;\
\    color: #44c5f2;\
\    background-color: transparent;\
\    font-size: 1.2em;\
\    text-align: left;\
\    text-decoration: none;\
\    display: inline-block;\
\    border: 0;\
\    padding: 0;\
\}\
\\
\.rawExceptionStackTrace {\
\    font-size: 1.2em;\
\}\
\\
\.rawExceptionBlock {\
\    border-top: 1px #ddd solid;\
\    border-bottom: 1px #ddd solid;\
\}\
\\
\.showRawExceptionContainer {\
\    margin-top: 10px;\
\    margin-bottom: 10px;\
\}\
\\
\.expandCollapseButton {\
\    cursor: pointer;\
\    float: left;\
\    height: 16px;\
\    width: 16px;\
\    font-size: 10px;\
\    position: absolute;\
\    left: 10px;\
\    background-color: #eee;\
\    padding: 0;\
\    border: 0;\
\    margin: 0;\
\}\
\\
\        </style>\
\    </head>\
\    <body>\
\        <h1>An unhandled exception occurred while processing the request.</h1>\
\            <div class=\"titleerror\">FileNotFoundException: Could not find file: ../../../../result.rvt</div>\
\                <p class=\"location\">Microsoft.AspNetCore.Mvc.Infrastructure.VirtualFileResultExecutor.ExecuteAsync(ActionContext context, VirtualFileResult result)</p>\
\        <ul id=\"header\">\
\            <li id=\"stack\" tabindex=\"1\" class=\"selected\">\
\                Stack\
\            </li>\
\            <li id=\"query\" tabindex=\"2\">\
\                Query\
\            </li>\
\            <li id=\"cookies\" tabindex=\"3\">\
\                Cookies\
\            </li>\
\            <li id=\"headers\" tabindex=\"4\">\
\                Headers\
\            </li>\
\        </ul>\
\\
\        <div id=\"stackpage\" class=\"page\">\
\            <ul>\
\                                    <li>\
\                        <h2 class=\"stackerror\">FileNotFoundException: Could not find file: ../../../../result.rvt</h2>\
\                        <ul>\
\                            <li class=\"frame\" id=\"frame1\">\
\                                    <h3>Microsoft.AspNetCore.Mvc.Infrastructure.VirtualFileResultExecutor.ExecuteAsync(ActionContext context, VirtualFileResult result)</h3>\
\\
\                            </li>\
\                            <li class=\"frame\" id=\"frame2\">\
\                                    <h3>Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultAsync(IActionResult result)</h3>\
\\
\                            </li>\
\                            <li class=\"frame\" id=\"frame3\">\
\                                    <h3>Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResultFilterAsync&lt;TFilter, TFilterAsync&gt;()</h3>\
\\
\                            </li>\
\                            <li class=\"frame\" id=\"frame4\">\
\                                    <h3>Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext context)</h3>\
\\
\                            </li>\
\                            <li class=\"frame\" id=\"frame5\">\
\                                    <h3>Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.ResultNext&lt;TFilter, TFilterAsync&gt;(ref State next, ref Scope scope, ref object state, ref bool isCompleted)</h3>\
\\
\                            </li>\
\                            <li class=\"frame\" id=\"frame6\">\
\                                    <h3>Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultFilters()</h3>\
\\
\                            </li>\
\                            <li class=\"frame\" id=\"frame7\">\
\                                    <h3>Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()</h3>\
\\
\                            </li>\
\                            <li class=\"frame\" id=\"frame8\">\
\                                    <h3>Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)</h3>\
\\
\                            </li>\
\                            <li class=\"frame\" id=\"frame9\">\
\                                    <h3>Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)</h3>\
\\
\                            </li>\
\                            <li class=\"frame\" id=\"frame10\">\
\                                    <h3>Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()</h3>\
\\
\                            </li>\
\                            <li class=\"frame\" id=\"frame11\">\
\                                    <h3>Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()</h3>\
\\
\                            </li>\
\                            <li class=\"frame\" id=\"frame12\">\
\                                    <h3>Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)</h3>\
\\
\                            </li>\
\                            <li class=\"frame\" id=\"frame13\">\
\                                    <h3>Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)</h3>\
\\
\                            </li>\
\                            <li class=\"frame\" id=\"frame14\">\
\                                    <h3>Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)</h3>\
\\
\                            </li>\
\                            <li class=\"frame\" id=\"frame15\">\
\                                    <h3>Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)</h3>\
\\
\                            </li>\
\                            <li class=\"frame\" id=\"frame16\">\
\                                    <h3>Microsoft.AspNetCore.SpaServices.Webpack.ConditionalProxyMiddleware.Invoke(HttpContext context)</h3>\
\\
\                            </li>\
\                            <li class=\"frame\" id=\"frame17\">\
\                                    <h3>Microsoft.AspNetCore.SpaServices.Webpack.ConditionalProxyMiddleware.Invoke(HttpContext context)</h3>\
\\
\                            </li>\
\                            <li class=\"frame\" id=\"frame18\">\
\                                    <h3>Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)</h3>\
\\
\                            </li>\
\                        </ul>\
\                    </li>\
\                    <li>\
\                        <br/>\
\                        <div class=\"rawExceptionBlock\">\
\                            <div class=\"showRawExceptionContain

其中最重要的部分可能是FileNotFoundException: Could not find file: ../../../../result.rvt。我已经验证了该文件相对于 approot 位于该位置,并且还尝试查看如果我尝试用“result.rvt”替换整个 FileVirtualPath 会发生什么,但这实际上并没有任何区别。当我使用调试器时 res 最终包含一个文件但没有修改日期表明实际文件可能不存在?有人知道如何解决这个问题吗?

【问题讨论】:

  • 能否请您粘贴 FileVirtualPath 的值
  • @CodeConstruct fileVirtualPath "../../../../result.rvt"
  • 输出 -----../../../../result.rvt 不是预期位置。应该重新解决这个问题
  • @CodeConstruct 你是什么意思?程序集位于 A:\code\Workspaces\csa\App\bin\Debug\net461 中,result.rvt 比它高 4 级。

标签: javascript c# vue.js asp.net-core file-transfer


【解决方案1】:

所以事实证明我应该使用PhysicalFile 和完整路径,而不是相对路径和File。解决此问题可停止错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-27
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    • 2023-03-31
    • 2013-01-23
    相关资源
    最近更新 更多