2016-08-10 74 views
0

我想从Java脚本的浏览器的新窗口中打开PDF文件。我不能使用window.open,因为这个文件位于跨域。 我试图通过process.Start()功能通过控制器做,什么都没有发生从跨域打开PDF文件

var p = new Process(); 
    p.StartInfo = new ProcessStartInfo(url); 
    p.StartInfo.WorkingDirectory = Path.GetDirectoryName(url); 
    p.Start(); 

(因为我的Chrome版本?) 没有任何人有好主意,对我? 非常感谢!

+2

为什么你不能使用window.open()?应该可以很好地从另一个域打开一个文件.... – epascarello

+0

不,它不.... –

+0

如果window.open没有打开到另一个页面的链接,那么比你有其他问题。 – epascarello

回答

0

我做了这样的: 我用Pekita想法(把文件放在我的程序目录中),然后我可以在关闭窗口后使用window.open(),我可以从我的位置删除这个文件。我的API控制器:

public string Get(string url,string fileNumber) 
    { 
     var urlInCurrDomain = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Documents", Path.GetFileNameWithoutExtension(url) +"-" + fileNumber + Path.GetExtension(url)); 
     if (File.Exists(urlInCurrDomain)) 
     { 
      urlInCurrDomain = TomerUtils.AddSuffix(urlInCurrDomain, 1); 
     } 

     File.Copy(url, urlInCurrDomain); 
     return Path.GetFileName(urlInCurrDomain); 

    } 

    public void Delete(string fileName) 
    { 
     var path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Documents", fileName); 
     if (File.Exists(path)) 
      File.Delete(path); 
    } 

我的Java脚本代码:

function copyFileToDomain(url) { 

$.ajax({ 
    type: "Get", 
    url: "/Tomer/api/Mismachimapi?url=" + url + "&fileNumber=" + fileCounter, 
    xhrFields: { withCredentials: true }, 
    crossDomain: true, 
    success: function (fileName) { 
     showFileWindow(fileName); 
    }, 
    error: function (xhr, status, error) { 
     showError(xhr, error, "err") 
    } 
}); 

} 打开的窗口中:

function showFileWindow(fileName) { 
var wnd = window.open("Documents/" + fileName, "_blank", "x=y"); 
wnd.onbeforeunload = function() { 
    deleteFile(fileName); 
}; 

}

删除文件:

function deleteFile(fileName) { 

$.ajax({ 
    type: "Delete", 
    url: "/Tomer/api/Mismachimapi?fileName=" + fileName, 
    xhrFields: { withCredentials: true }, 
    crossDomain: true, 
    error: function (xhr, status, error) { 
     showError(xhr, error, "err") 
    } 
}); 

}

0

如果你想从JavaScript做它,你只需要添加一个HttpHandler或简单WebForm将使用HttpClient获取跨域PDF文件,并使用Response.WriteFile()方法写入文件内容。 并添加来自JavaScript或使用锚标签的httphandlerwebform页面的呼叫。

0

如果你把文件放在你的程序dirctory,那么你可以使用此代码

string path = AppDomain.CurrentDomain.BaseDirectory.ToString() + "YourPDF"; 

,然后欧做到这一点

var p = new Process(); 
     p.StartInfo = new ProcessStartInfo(path); 
     p.StartInfo.WorkingDirectory = Path.GetDirectoryName(path); 
     p.Start(); 
+0

我该如何移动它?我在远程文件夹中有很多文件,我可以将特定文件复制到我的程序中吗? –

+0

只是把文件和程序放在同一个目录@שולמיתשרשבסקי – Pekita

+0

@שולמיתשרשבסקיdid it work? – Pekita