在javascript中如何从服务器打开文件? [英] In javascript how do you open a file from the server?

查看:139
本文介绍了在javascript中如何从服务器打开文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个在服务器端使用java + spring的应用程序以及extdirectspring工作。在客户端,它使用extjs / javascript。



我想在服务器端戳一个方法,并从数据库中检索一个文件。如果文件不存在,我想以某种方式显示错误。



检索文件和检查它的存在的尝试需要发生在相同的电话 - 文件可以在呼叫之间删除。



我可以看到人们在当前应用程序中完成的方式是使用弹簧控制器+请求映射和这似乎不让你处理文件不存在的情况。



理想情况下,我只想从服务器发送一些json具有文件数据(可能为空)和成功/失败。当我得到响应时,我可以显示有关失败的信息或打开文件。不幸的是,我无法观察当前的故障模式,因为某些地方缓存文件 - 如果我们从数据库中删除它们,那么它们似乎仍然存在,您仍然可以下载它们。



通过'open'我的意思是显示标准'你想要做这个文件打开/保存'对话框。我不是要解析文件或做任何事情 - 我只想把它提供给浏览器/用户。



有没有办法没有使用url和window.open?例如某些方法可以获取数据和文件名或类似的数据?



更新



数据/ json的传输不是我想要解决的问题。



由于我正在使用extdirect,我可能会做它像这样:

  public class SomeClass 
{
@ExtDirectMethod
public AFile getFile Long id)throws Exception
{
// do stuff
}
}

然后在客户端你只需要:

  someClass.getFile(id,function(file){ 
if(file.found){
SomeHowGiveThisToTheUser(file.name,file.data);?
return;
}

ReportCouldntFind (file.name);
});

我不知道该怎么办的是将文件提供给用户



进一步更新



我不认为可以这样做没有blob网址或数据uri的。这两个都在此帖中提及。我没有尝试过,因为我们不得不支持两种技术的旧版浏览器。

解决方案

你是想要做一些标准的Ajax(假设你有jQuery可用)。



这样的东西:

  $。getJSON(url,function(data){
if(data.success){

} else {

}
});

在服务器端,添加代码返回预期的JSON。



在extJS中:

  Ext.Ajax.request({
url:url,
方法:'GET',
标题:{'Content-Type':'application / json'},
params:params,
success:function(response){
var jsonResp = Ext.util.JSON.decode(response.responseText);
if(response.success){
//做成功的东西,像使用response.fileData
} else {
// do fail stuff
}
failure:function(response){
var jsonResp = Ext.util.JSON.decode(response.responseText);
//等
});

在Java servlet的服务器上,您可以这样(假定apache commons file util):

  protected void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException {
InputStream in = new URL(http://remote.file.url).openStream();
IOUtils.copy(in,response.getOutputStream());
}

可能是一个更春天的具体方式来做,但这给你一个想法



对于您的情况,您希望将文件内容包含在包含成功的JSON对象中。为此,请使用Java JSON jar: http://json.org/java/






更新:最后了解您的要求。



这几天有几种方法。



看看这个SO答案,这是使用iFrame:下载文件使用Javascript / jQuery



还有几个花哨的下载器组件,其中包括几个jQuery。



所以你会做一个两部分的过程:使用标准的ajax调用检查文件的可用性,然后如果 response.success ,使用下载器将文件提供给用户。


Im working in an application that uses java + spring on the server side aswell as extdirectspring. On the client side it uses extjs/javascript.

I want to poke a method on the serverside and retrieve a file from the database. If the file doesn't exist then I'd like to display an error in some way.

The attempt to retrieve the file and the check it exists need to happen in the same call - the file could be deleted in between calls.

The way I can see people have done it in the current application is using spring controllers + request mappings and a window.open("someUrl/filename.blah"); with the server returning the file from the mapped method.

This doesnt seem to let you handle the case where the file doesn't exist though.

Ideally I'd just like to send some json back from the server which has the file data (possibly null) and sucess/failure. When I get the response I can then either show some information about the failure or open the file. Unfortunately I can't observe the current failure mode because something somewhere is caching the files - if I delete them from the database then they appear to still exist and you can still download them!

By 'open' I mean show the standard 'what do you want to do with this file open/save' dialog. I'm not trying to parse the file or do anything with it - I just want to serve it up to the browser/user.

Is there a way to do that without using a url and window.open? Eg some method that takes a blob of data and a file name or similar?

Update

The transfer of the data/json isn't the problem I'm trying to solve.

As I'm using extdirect I'll probably just do it like this:

public class SomeClass
{
    @ExtDirectMethod
    public AFile getFile(Long id) throws Exception
    {
        //do stuff
    }
}

Then on the clientside you just do:

someClass.getFile(id, function(file){
    if(file.found){
        SomeHowGiveThisToTheUser(file.name,file.data);  ????
        return;
    }

    ReportCouldntFind(file.name);
});

The bit I dont know how to do is the give the file to the user.

Further update

I dont think it's possible to do this without blob urls or data uri's. Both of these are mentioned in this post. I haven't tried them out as we are having to support a browser that is too old for both techniques.

解决方案

You are wanting to do some standard Ajax (Assuming you have jQuery available).

Something like:

$.getJSON( url, function( data ) {
  if (data.success){

  } else {

  }
});

And on the server side, add code to return the expected JSON.

In extJS:

Ext.Ajax.request({
  url : url,
  method: 'GET',
  headers: { 'Content-Type': 'application/json' },                     
  params : params,
  success: function (response) {
         var jsonResp = Ext.util.JSON.decode(response.responseText);
         if (response.success){
           // do success stuff, like using response.fileData
         } else {
           // do fail stuff
         }
  failure: function (response) {
      var jsonResp = Ext.util.JSON.decode(response.responseText);
      // etc.
 });

On the server in a Java servlet you could something like this (assumes apache commons file util):

protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
  InputStream in = new URL( "http://remote.file.url" ).openStream();
  IOUtils.copy(in, response.getOutputStream());
}

Probably a more spring specific way to do it, but that gives you an idea.

For your case, you want to wrap the file contents in a JSON object that includes the success proeprty. For that, use the Java JSON jar: http://json.org/java/


Update: Finally understand what you are asking.

It finally occurred to me that you are asking how to handle the actual download.

There's a couple ways these days.

Take a look at this SO answer, this is using an iFrame: Download File Using Javascript/jQuery

There are also several fancy downloader components, including several for jQuery.

So you would do a two part process: check for the file availability using a standard ajax call, and then if response.success, use the downloader to serve the file to the user.

这篇关于在javascript中如何从服务器打开文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆