【问题标题】:Send a file to an mvc controller from an ASPX page从 ASPX 页面向 mvc 控制器发送文件
【发布时间】:2018-04-01 00:31:26
【问题描述】:

我正在使用网络扫描解决方案,并且正在尝试将文件从 aspx 页面重定向到 mvc 控制器。扫描的图像可以很好地进入 aspx 页面,但我不知道如何将文件获取到 MVC 控制器。 这是我的代码:

            HttpFileCollection files = HttpContext.Current.Request.Files;
            HttpPostedFile uploadfile = files["RemoteFile"];string fileName = uploadfile.FileName;
            string seqNum = HttpContext.Current.Request.Form["sequenceNum"];
            string imageIndex = HttpContext.Current.Request.Form["imageIndex"];
            string docId = HttpContext.Current.Request.Form["docId"];
            string guid = HttpContext.Current.Request.Form["guid"];
            string url = "/controller/action/" + guid + "?fileName=" + fileName + "&imageIndex=" + imageIndex + "&sequenceNum=" + seqNum + "&docId=" + docId;

我尝试对 url 中的文件使用 Response.Redirect,但它一直被丢弃。该文件已被扫描,并且在客户端计算机上没有保存的路径(潜在的医疗记录 + HIPAA 使其成为禁忌)。我错过了什么?


我尝试重新设计路由,现在scanner.aspx 接管了所有路由。路由方法如下:

public static void RegisterRoutes(RouteCollection routes) {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapPageRoute("", "scanner.aspx", "~/UtilityClasses/scanner.aspx");
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{guid}",
            defaults: new { controller = "Login", action = "Index", guid = UrlParameter.Optional }
        );
    }

这是经过多番探索后的最新更新。

public static void RegisterRoutes(RouteCollection routes) {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapPageRoute("", "scanner.aspx", "~/TextDocuments/GetScannedDocument");
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{guid}",
            defaults: new { controller = "Login", action = "Index", guid = UrlParameter.Optional }
        );
    }

控制器名称是TextDocumentsController,动作是GetScannedDocument


这东西正在变成一本书……我仔细查看了一些附加的示例,我让 MapPageRoute 工作了。但我仍在查看我的scanner.aspx.cs 页面,我不确定如何将HttpPostedFile 获取到我的MVC 控制器。


这是我现在的 MapPageRoute。它仍然将我发送到 aspx 页面上的代码而不是 MVC 控制器。我错过了什么?

        routes.MapPageRoute("Scanner",
            "TextDocuments/GetScannedDocument",
            "~/UtilityClasses/scanner.aspx", 
            true, null, 
            new RouteValueDictionary { { "incoming", new MyCustomConstaint()} });

【问题讨论】:

  • 控制器和aspx页面是在同一个网站还是不同网站?
  • 同一个网站。如果可以的话,我会避免使用 ASPX 页面,但是我们使用的扫描解决方案需要它。
  • 为什么不将 ASPX 的路径直接映射到动作?对于扫描解决方案来说,它仍然看起来像一个 ASPX 页面,但实际上会将流量路由到您的控制器。
  • 当我执行 MapPageRoute 时,它​​会接管,我所能做的就是登录。
  • routes.MapPageRoute("", "scanner.aspx", "~/TextDocuments/GetScannedDocument"); 你的第二个和第三个参数是错误的。查看stackoverflow.com/a/10178849/34092。你可能想要routes.MapPageRoute("", "TextDocuments/GetScannedDocument", "~/whateverthepathis/scanner.aspx");

标签: c# asp.net asp.net-mvc


【解决方案1】:

使用 Server.MapPath

public ViewResult ShowForm()
{
    //Logo
    ViewBag.Img = Server.MapPath("~") + @"Content\Images\Logo.png";
    return View("ShowForm");
}

【讨论】:

    猜你喜欢
    • 2015-03-12
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 2020-12-26
    • 1970-01-01
    • 2016-03-16
    • 2015-01-04
    • 2012-09-07
    相关资源
    最近更新 更多