我做了这样的: 我用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")
}
});
}
为什么你不能使用window.open()?应该可以很好地从另一个域打开一个文件.... – epascarello
不,它不.... –
如果window.open没有打开到另一个页面的链接,那么比你有其他问题。 – epascarello