在新标签页中打开.pdf文件

时间:2019-06-11 11:20:25

标签: c# jquery asp.net-mvc

在我的表单中,我有一个ddl,用户可以在一些pdf文件之间进行选择。
我想将用户选择的ID(包含pdf的完整路径)传递给window.open命令。

//This is the div that contains the data
        <div class="form-group">
            <div class="row">
                <div class="col-xs-12 col-sm-6 col-md-6 col-lg-4">
                    <label asp-for="FDescr" class="control-label">File</label>
                    <select asp-for="FCodeID" class="form-control"
                            asp-items="@(new SelectList(string.Empty, "FCodeID", "FDescr"))"></select>
                </div>
            </div>
        </div>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.js"></script>

//This is where I load the data in the #FCodeID ddl
<script type="text/javascript">
        $(document).ready(function () {
            $("#MCodeID").on("change", function () {
                var url = '@Url.Content("~/")' + "Materials/GetFiles";
                var ddlsource = "#MCodeID";
                var items = "<option value='0'>--Select File--</option>";
                $("#FCodeID").html(items);
                var items = " ";
                $.getJSON(url, { MCodeID: $(ddlsource).val() }, function (data) {
                    $("#FCodeID").empty();
                    $.each(data, function (i, file) {
                        if (file.value.includes("in Use"))
                            items += "<option value='" + file.value + "'" + "style='background-color:orange;'>" + file.text + "</option>";
                        else
                            items += "<option value='" + file.value + "'>" + file.text;
                    });
                    $("#FCodeID").html(items);
                });
            })
        })
    </script>

//This is what I'm trying to accomplish
    <script type="text/javascript">
        $(document).ready(function () {
            $("#FCodeID").on ("change", function () {
                var url= "#FCodeID";
                window.open(url, '_blank');
            })
        })
    </script>

我想在window.open()上打开一个包含我文件实际路径的新选项卡。
e.x 192.168.1.1/MyFiles/MyPDF.pdf
预先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

@ADyson提供的解决方案

<script type="text/javascript">
  $(document).ready(function () {
    $("#FCodeID").on ("change", function () {
      var url = "../MaterialsFiles" + this.value;;
      window.open(url, '_blank');
    })
  })
</script>

此外,我像这样更改JsonResult中的.value字符串:

public JsonResult GetFiles(string MCodeID)
{
    List<Files> filelist = new List<Files>();
    //Searching Files in //192.168.1.191
    string path = @"path to file srv";
    string searchPattern = MCodeID + "*";
    DirectoryInfo fi = new DirectoryInfo(path);
    foreach (var file in fi.GetFiles(searchPattern, SearchOption.AllDirectories))
    {
        var changeSlash = file.FullName.Replace('\\', '/');
        var filepath = changeSlash.Replace("path to file srv", "");
        filelist.Add(new Files
        {
            FCodeID = filepath,
            FDescr = file.Name + " - " + Math.Round((Convert.ToDouble(file.Length) / (1024 * 1024)), 2) + " MB",
        });
        }
    filelist.Insert(0, new Files { FCodeID = "0", FDescr = "--Select File--" });
    return Json(new SelectList(filelist, "FCodeID", "FDescr"));
}

最后,我添加一个新的虚拟像:

<site name="MaterialProject" id="2">
  <application path="/" applicationPool="Clr4IntegratedAppPool">
    <virtualDirectory path="/" physicalPath="path to solution local file" />
    <virtualDirectory path="/MaterialsFiles" physicalPath="path to file srv" />
  </application>
</site>