如何在Firefox和Safari上进行Flex文件上传工作?

时间:2022-02-16 07:07:28

I have a flex app that uploads files to a server. The server requires authentication to be able to upload. In IE the upload works fine. However in FF and Safari, it does not upload. I have seen people all over with this same problem but no answers. Don't fail me now *ers.

我有一个将文件上传到服务器的flex应用程序。服务器要求身份验证才能上载。在IE中,上传工作正常。但是在FF和Safari中,它不会上传。我看到同样的问题,但没有答案。不要让我失败现在*ers。

10 个解决方案

#1


3  

I found this question while trying to find the answer myself. The solution was rather simple.

我在试图找到答案时发现了这个问题。解决方案相当简单。

Based on the flash player bug that others have linked, and the comments on that page, I decided to append session identifiers to my upload URL and give it a shot. It really was that easy!

基于其他人链接的Flash播放器错误以及该页面上的评论,我决定将会话标识符附加到我的上传URL并为其提供一个镜头。真的很容易!

To make it work, I started by adding a flashVar parameter called sessionParams. This allowed me to pass any string I want in to the flash player as my session identifier, and it will later get appended to the URL used to upload.

为了使它工作,我开始添加一个名为sessionParams的flashVar参数。这允许我将我想要的任何字符串传递给flash播放器作为我的会话标识符,稍后它将附加到用于上传的URL。

//sessionParams - resolves firefox upload bug
public var sessionParams:String = "";

//...

public function initApp():void{
    sessionParams = Application.application.parameters.sessionParams;
}

In my case, I'm on ColdFusion with java sessions enabled, so my sessionParams are setup like the following before being passed into the flash player:

在我的情况下,我在ColdFusion上启用了java会话,所以我的sessionParams在被传递到flash播放器之前设置如下:

<cfset flashVars = "sessionParams=#urlEncodedFormat('jsessionid=' & session.sessionid)#" />

Don't forget to escape special characters like =,&, etc (which I've done with urlEncodedFormat), so that they are treated as part of the value of the "sessionParams" parameter, and not breakpoints to indicate other parameters. You're embedding future-URL information in the current URL.

不要忘记转义特殊字符,如=,&等(我用urlEncodedFormat完成),这样它们就被视为“sessionParams”参数值的一部分,而不是断点来指示其他参数。您将future-URL信息嵌入当前URL中。

Then, use the sessionParams value in your upload code. Here's a snippet of how I set mine up:

然后,在上传代码中使用sessionParams值。这是我如何设置我的一个片段:

// Set Up URLRequest
_uploadURL = new URLRequest;
_uploadURL.url = _url + "?" + _sessionParams;
_uploadURL.method = "GET";
_uploadURL.data = _variables;
_uploadURL.contentType = "multipart/form-data";

The variable names are different (but similar) because this is part of a reusable class.

变量名称不同(但相似),因为它是可重用类的一部分。

Hopefully that helps you. If not, let me know and I'll try to provide more code or explanation to help you out.

希望这对你有所帮助。如果没有,请告诉我,我会尝试提供更多代码或解释来帮助您。

#2


3  

The problem at least in Firefox is that the session cookies are not sent in the request when you invoke FileReference.upload(). What you need to do is add the authentication token either as a form variable or in the query string. Here is an example in Java where the session cookie is called "jsessionid"

至少在Firefox中的问题是,当您调用FileReference.upload()时,会话cookie不会在请求中发送。您需要做的是将身份验证令牌添加为表单变量或查询字符串。以下是Java中的一个示例,其中会话cookie称为“jsessionid”

var request : URLRequset = new URLRequest( uploadUrl + ";jsessionid=" + jsessionid);

You can parse the jsessionid out of cookies using Javascript and ExternalInterface to invoke the Javascript function. Or after you authenticate you can have Flex call a backend method that returns the current sessionID.

您可以使用Javascript和ExternalInterface从cookie中解析jsessionid以调用Javascript函数。或者在进行身份验证后,您可以让Flex调用一个返回当前sessionID的后端方法。

The related Flex bug is here:

相关的Flex错误在这里:

http://bugs.adobe.com/jira/browse/FP-201

#3


1  

I solved this problem. File upload using flex will work on all the browsers.In J2ee application,

我解决了这个问题。使用flex进行文件上传将适用于所有浏览器。在J2ee应用程序中,

comment the security-constraint or make the fileupload.do URL unprotected in web.xml where you will put the actual code.

注释security-constraint或使fileupload.do URL在web.xml中不受保护,您将在其中放置实际代码。

<security-constraint>
    <display-name>Senusion Security Constraint</display-name>
    <web-resource-collection>
        <web-resource-name>Un Protected Area</web-resource-name>
          <url-pattern>/fileupload.do</url-pattern>
      </web-resource-collection>
</security-constraint> 

Hope this will help the next reader.

希望这将有助于下一位读者。

#4


1  

FlashPlayer 10 provides a new Filereference API that can help a lot. Here is a blog entry that describes it : http://www.flexpasta.com/index.php/2010/02/21/uploading-files-with-firefox-solution/.

FlashPlayer 10提供了一个可以提供帮助的新Filereference API。这是一篇描述它的博客文章:http://www.flexpasta.com/index.php/2010/02/21/uploading-files-with-firefox-solution/。

Indeed in Flash 10 an enhancement to flash.net.FileReference makes it possible to read the contents of a file before it is uploaded. Meaning that the file can be uploaded in different ways then can be done in Flash 9. The following example shows how easy file uploading can be and is not tied to SSL, Firefox, IE, Chrome, etc.

实际上,在Flash 10中,对flash.net.FileReference的增强使得在上载文件之前可以读取文件的内容。这意味着文件可以以不同的方式上传,然后可以在Flash 9中完成。以下示例显示了文件上载的简单方式,并且与SSL,Firefox,IE,Chrome等无关。

#5


1  

i managed to work around this bug using flex and java web filter

我设法使用flex和java web过滤器来解决这个bug

Flex Code :

Flex代码:

var urlVars:URLVariables = new URLVariables();
urlVars.jsessionid = sessionID;

var uploadUrl:String = "http://localhost:8080/mywar;jsessionid="+sessionID;
uploadUrl += "?"+getClientCookies(); //put all client cookies on the query string 
var urlRequest:URLRequest = new URLRequest(uploadUrl);
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = urlVars;

//will go first time and get the cookies set see flex docs  
var testUpload:Boolean = true; 
fileRef.upload(urlRequest,"Filedata",testUpload);

JAVA CODE :

JAVA代码:

package com.mywar.fileupload;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author orasio - spieler
 * This filter comes to solve the Firefox ,Chrome and SAFARI file upload issue
 * The problem was that the file uploaded by the flex 
 * FileReference came with a different session and no cookies
 * To solve this problem do the following : 
 * 
 * 
 * don't forget to add this filter to the web.xml file
 */
public class FileUploadFilter implements Filter {

    private static final String CONTENT_LENGTH = "content-length";
    private static final String UPLOAD_SITE_PATH = "/";
    private static final String JSESSIONID = "JSESSIONID";


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, 
                 ServletResponse response,
                 FilterChain filterChain) 
                 throws IOException, ServletException {
        if ((request instanceof HttpServletRequest) 
         && (response instanceof HttpServletResponse)) {
            HttpServletRequest httpRequest = (HttpServletRequest) request;

            //httpRequest.getHeader("user-agent");  //Shockwave Flash
            String contentLength = httpRequest.getHeader(CONTENT_LENGTH);
            boolean isFlexTest = (contentLength!=null 
                      && Integer.parseInt(contentLength)==0); 
            if(isFlexTest){ 
               HttpServletResponse httpResponse = 
                                            (HttpServletResponse) response;
               setAllClientCookie((HttpServletResponse)response, httpRequest);
               PrintWriter out = httpResponse.getWriter();
               out.println("OK");
               out.close();
               return;
            }
        }
        filterChain.doFilter(request, response);
    }

    /*
     * write all cookies back to the flex test response 
     */
    @SuppressWarnings("unchecked")
    private void setAllClientCookie(HttpServletResponse httpResponse,
                    HttpServletRequest httpRequest) {
        Enumeration<String> parameterNames = 
                (Enumeration<String>)httpRequest.getParameterNames();
        while (parameterNames.hasMoreElements()) {
           String cookieName = (String) parameterNames.nextElement();
     //since we get IllegalArgumentException: Cookie name "JSESSIONID" is a reserved token

           if(!cookieName.contains(JSESSIONID)) { 
              Cookie cookie = 
                          new Cookie(cookieName, httpRequest.getParameter(cookieName));
              cookie.setPath(UPLOAD_SITE_PATH);
              httpResponse.addCookie(cookie);
           }
        }
    }

    @Override
    public void destroy() {
    }

}

#6


1  

I faced the same issue.. File upload was working on all browsers except firefox. In firefox, error#2038 was being thrown while uploading file. The application used SSL.. In my case, even the upload request wasn't being generated from firefox which I could confirm by seeing in firebug's Net panel, the upload URL was not being hit. That means, may be flash runtime in firefox was blocking the upload request. However, when I ran application in IE, installed the self signed certificate of the application in IE, file upload ambigously and ofcourse amazingly, started working in firefox.. So first please check whether request is even being reached to the server or getting blocked at the client.

我遇到了同样的问题..文件上传正在除Firefox之外的所有浏览器上运行。在firefox中,上传文件时出现错误#2038。该应用程序使用SSL ..在我的情况下,甚至上传请求都不是从firefox生成的,我可以通过在firebug的Net面板中查看来确认,上传的URL没有被点击。这意味着,可能是firefox中的flash运行时阻止了上传请求。但是,当我在IE中运行应用程序时,在IE中安装了应用程序的自签名证书,文件上传模糊和令人惊讶,开始在firefox中工作..所以首先请检查请求是否甚至被连接到服务器或被阻止客户端。

Thanks

#7


0  

Looks like this is quite old, but I recently ran into this problem, too. My fix (which is far from optimal) under a Flex + authenticated rails setup was to turn off the session based authentication on the upload script.

看起来这已经很老了,但我最近遇到了这个问题。在Flex +经过身份验证的rails设置下,我的修复(远非最佳)是关闭上载脚本中基于会话的身份验证。

Since I really did want at least basic authentication, I stored the username and password that the user logged in with, and wrote the code to send/validate that manually on the rails side. I could never get the "jsessionid" hack to work, as flash doesn't have access to the browser sessions.

由于我确实至少需要基本身份验证,因此我存储了用户登录的用户名和密码,并编写了代码以便在rails端手动发送/验证。我永远无法让“jsessionid”黑客工作,因为Flash无法访问浏览器会话。

I hope this helps someone save a bit of time.

我希望这可以帮助别人节省一点时间。

#8


0  

This is an actual flash player bug. Maybe this link will give you some ideas.

这是一个真正的Flash播放器错误。也许这个链接会给你一些想法。

What do you have on the server side? Maybe you could add the sessionid as a parameter in your request.

你在服务器端有什么?也许你可以在你的请求中添加sessionid作为参数。

#9


0  

Some times even if we send the cookies through the URL it will not work. This is because the Flex is blocking the file upload request.

有时即使我们通过URL发送cookie也无效。这是因为Flex阻止了文件上载请求。

To unblock it you have to install the SSL certificate, and then try it.

要取消阻止它,您必须安装SSL证书,然后尝试它。

If any one has any other answer please let me know.

如果有人有任何其他答案,请告诉我。

#10


0  

Since I was building a Flash App for Facebook, I had no access to jsessionid.

自从我为Facebook构建Flash应用程序以来,我无法访问jsessionid。

I solved this problem by uploading to a HTTPS address instead of HTTP.

我通过上传到HTTPS地址而不是HTTP来解决了这个问题。

One thing that caused me trouble is that in OSX Firefox and Safari (not Chrome), the (FileReferenceInstance).type is null, and the (FileReferenceInstance).name comes with the full extension (myimage.jpg).

导致我麻烦的一件事是在OSX Firefox和Safari(不是Chrome)中,(FileReferenceInstance).type为null,而(FileReferenceInstance).name附带完整扩展名(myimage.jpg)。

#1


3  

I found this question while trying to find the answer myself. The solution was rather simple.

我在试图找到答案时发现了这个问题。解决方案相当简单。

Based on the flash player bug that others have linked, and the comments on that page, I decided to append session identifiers to my upload URL and give it a shot. It really was that easy!

基于其他人链接的Flash播放器错误以及该页面上的评论,我决定将会话标识符附加到我的上传URL并为其提供一个镜头。真的很容易!

To make it work, I started by adding a flashVar parameter called sessionParams. This allowed me to pass any string I want in to the flash player as my session identifier, and it will later get appended to the URL used to upload.

为了使它工作,我开始添加一个名为sessionParams的flashVar参数。这允许我将我想要的任何字符串传递给flash播放器作为我的会话标识符,稍后它将附加到用于上传的URL。

//sessionParams - resolves firefox upload bug
public var sessionParams:String = "";

//...

public function initApp():void{
    sessionParams = Application.application.parameters.sessionParams;
}

In my case, I'm on ColdFusion with java sessions enabled, so my sessionParams are setup like the following before being passed into the flash player:

在我的情况下,我在ColdFusion上启用了java会话,所以我的sessionParams在被传递到flash播放器之前设置如下:

<cfset flashVars = "sessionParams=#urlEncodedFormat('jsessionid=' & session.sessionid)#" />

Don't forget to escape special characters like =,&, etc (which I've done with urlEncodedFormat), so that they are treated as part of the value of the "sessionParams" parameter, and not breakpoints to indicate other parameters. You're embedding future-URL information in the current URL.

不要忘记转义特殊字符,如=,&等(我用urlEncodedFormat完成),这样它们就被视为“sessionParams”参数值的一部分,而不是断点来指示其他参数。您将future-URL信息嵌入当前URL中。

Then, use the sessionParams value in your upload code. Here's a snippet of how I set mine up:

然后,在上传代码中使用sessionParams值。这是我如何设置我的一个片段:

// Set Up URLRequest
_uploadURL = new URLRequest;
_uploadURL.url = _url + "?" + _sessionParams;
_uploadURL.method = "GET";
_uploadURL.data = _variables;
_uploadURL.contentType = "multipart/form-data";

The variable names are different (but similar) because this is part of a reusable class.

变量名称不同(但相似),因为它是可重用类的一部分。

Hopefully that helps you. If not, let me know and I'll try to provide more code or explanation to help you out.

希望这对你有所帮助。如果没有,请告诉我,我会尝试提供更多代码或解释来帮助您。

#2


3  

The problem at least in Firefox is that the session cookies are not sent in the request when you invoke FileReference.upload(). What you need to do is add the authentication token either as a form variable or in the query string. Here is an example in Java where the session cookie is called "jsessionid"

至少在Firefox中的问题是,当您调用FileReference.upload()时,会话cookie不会在请求中发送。您需要做的是将身份验证令牌添加为表单变量或查询字符串。以下是Java中的一个示例,其中会话cookie称为“jsessionid”

var request : URLRequset = new URLRequest( uploadUrl + ";jsessionid=" + jsessionid);

You can parse the jsessionid out of cookies using Javascript and ExternalInterface to invoke the Javascript function. Or after you authenticate you can have Flex call a backend method that returns the current sessionID.

您可以使用Javascript和ExternalInterface从cookie中解析jsessionid以调用Javascript函数。或者在进行身份验证后,您可以让Flex调用一个返回当前sessionID的后端方法。

The related Flex bug is here:

相关的Flex错误在这里:

http://bugs.adobe.com/jira/browse/FP-201

#3


1  

I solved this problem. File upload using flex will work on all the browsers.In J2ee application,

我解决了这个问题。使用flex进行文件上传将适用于所有浏览器。在J2ee应用程序中,

comment the security-constraint or make the fileupload.do URL unprotected in web.xml where you will put the actual code.

注释security-constraint或使fileupload.do URL在web.xml中不受保护,您将在其中放置实际代码。

<security-constraint>
    <display-name>Senusion Security Constraint</display-name>
    <web-resource-collection>
        <web-resource-name>Un Protected Area</web-resource-name>
          <url-pattern>/fileupload.do</url-pattern>
      </web-resource-collection>
</security-constraint> 

Hope this will help the next reader.

希望这将有助于下一位读者。

#4


1  

FlashPlayer 10 provides a new Filereference API that can help a lot. Here is a blog entry that describes it : http://www.flexpasta.com/index.php/2010/02/21/uploading-files-with-firefox-solution/.

FlashPlayer 10提供了一个可以提供帮助的新Filereference API。这是一篇描述它的博客文章:http://www.flexpasta.com/index.php/2010/02/21/uploading-files-with-firefox-solution/。

Indeed in Flash 10 an enhancement to flash.net.FileReference makes it possible to read the contents of a file before it is uploaded. Meaning that the file can be uploaded in different ways then can be done in Flash 9. The following example shows how easy file uploading can be and is not tied to SSL, Firefox, IE, Chrome, etc.

实际上,在Flash 10中,对flash.net.FileReference的增强使得在上载文件之前可以读取文件的内容。这意味着文件可以以不同的方式上传,然后可以在Flash 9中完成。以下示例显示了文件上载的简单方式,并且与SSL,Firefox,IE,Chrome等无关。

#5


1  

i managed to work around this bug using flex and java web filter

我设法使用flex和java web过滤器来解决这个bug

Flex Code :

Flex代码:

var urlVars:URLVariables = new URLVariables();
urlVars.jsessionid = sessionID;

var uploadUrl:String = "http://localhost:8080/mywar;jsessionid="+sessionID;
uploadUrl += "?"+getClientCookies(); //put all client cookies on the query string 
var urlRequest:URLRequest = new URLRequest(uploadUrl);
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = urlVars;

//will go first time and get the cookies set see flex docs  
var testUpload:Boolean = true; 
fileRef.upload(urlRequest,"Filedata",testUpload);

JAVA CODE :

JAVA代码:

package com.mywar.fileupload;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author orasio - spieler
 * This filter comes to solve the Firefox ,Chrome and SAFARI file upload issue
 * The problem was that the file uploaded by the flex 
 * FileReference came with a different session and no cookies
 * To solve this problem do the following : 
 * 
 * 
 * don't forget to add this filter to the web.xml file
 */
public class FileUploadFilter implements Filter {

    private static final String CONTENT_LENGTH = "content-length";
    private static final String UPLOAD_SITE_PATH = "/";
    private static final String JSESSIONID = "JSESSIONID";


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, 
                 ServletResponse response,
                 FilterChain filterChain) 
                 throws IOException, ServletException {
        if ((request instanceof HttpServletRequest) 
         && (response instanceof HttpServletResponse)) {
            HttpServletRequest httpRequest = (HttpServletRequest) request;

            //httpRequest.getHeader("user-agent");  //Shockwave Flash
            String contentLength = httpRequest.getHeader(CONTENT_LENGTH);
            boolean isFlexTest = (contentLength!=null 
                      && Integer.parseInt(contentLength)==0); 
            if(isFlexTest){ 
               HttpServletResponse httpResponse = 
                                            (HttpServletResponse) response;
               setAllClientCookie((HttpServletResponse)response, httpRequest);
               PrintWriter out = httpResponse.getWriter();
               out.println("OK");
               out.close();
               return;
            }
        }
        filterChain.doFilter(request, response);
    }

    /*
     * write all cookies back to the flex test response 
     */
    @SuppressWarnings("unchecked")
    private void setAllClientCookie(HttpServletResponse httpResponse,
                    HttpServletRequest httpRequest) {
        Enumeration<String> parameterNames = 
                (Enumeration<String>)httpRequest.getParameterNames();
        while (parameterNames.hasMoreElements()) {
           String cookieName = (String) parameterNames.nextElement();
     //since we get IllegalArgumentException: Cookie name "JSESSIONID" is a reserved token

           if(!cookieName.contains(JSESSIONID)) { 
              Cookie cookie = 
                          new Cookie(cookieName, httpRequest.getParameter(cookieName));
              cookie.setPath(UPLOAD_SITE_PATH);
              httpResponse.addCookie(cookie);
           }
        }
    }

    @Override
    public void destroy() {
    }

}

#6


1  

I faced the same issue.. File upload was working on all browsers except firefox. In firefox, error#2038 was being thrown while uploading file. The application used SSL.. In my case, even the upload request wasn't being generated from firefox which I could confirm by seeing in firebug's Net panel, the upload URL was not being hit. That means, may be flash runtime in firefox was blocking the upload request. However, when I ran application in IE, installed the self signed certificate of the application in IE, file upload ambigously and ofcourse amazingly, started working in firefox.. So first please check whether request is even being reached to the server or getting blocked at the client.

我遇到了同样的问题..文件上传正在除Firefox之外的所有浏览器上运行。在firefox中,上传文件时出现错误#2038。该应用程序使用SSL ..在我的情况下,甚至上传请求都不是从firefox生成的,我可以通过在firebug的Net面板中查看来确认,上传的URL没有被点击。这意味着,可能是firefox中的flash运行时阻止了上传请求。但是,当我在IE中运行应用程序时,在IE中安装了应用程序的自签名证书,文件上传模糊和令人惊讶,开始在firefox中工作..所以首先请检查请求是否甚至被连接到服务器或被阻止客户端。

Thanks

#7


0  

Looks like this is quite old, but I recently ran into this problem, too. My fix (which is far from optimal) under a Flex + authenticated rails setup was to turn off the session based authentication on the upload script.

看起来这已经很老了,但我最近遇到了这个问题。在Flex +经过身份验证的rails设置下,我的修复(远非最佳)是关闭上载脚本中基于会话的身份验证。

Since I really did want at least basic authentication, I stored the username and password that the user logged in with, and wrote the code to send/validate that manually on the rails side. I could never get the "jsessionid" hack to work, as flash doesn't have access to the browser sessions.

由于我确实至少需要基本身份验证,因此我存储了用户登录的用户名和密码,并编写了代码以便在rails端手动发送/验证。我永远无法让“jsessionid”黑客工作,因为Flash无法访问浏览器会话。

I hope this helps someone save a bit of time.

我希望这可以帮助别人节省一点时间。

#8


0  

This is an actual flash player bug. Maybe this link will give you some ideas.

这是一个真正的Flash播放器错误。也许这个链接会给你一些想法。

What do you have on the server side? Maybe you could add the sessionid as a parameter in your request.

你在服务器端有什么?也许你可以在你的请求中添加sessionid作为参数。

#9


0  

Some times even if we send the cookies through the URL it will not work. This is because the Flex is blocking the file upload request.

有时即使我们通过URL发送cookie也无效。这是因为Flex阻止了文件上载请求。

To unblock it you have to install the SSL certificate, and then try it.

要取消阻止它,您必须安装SSL证书,然后尝试它。

If any one has any other answer please let me know.

如果有人有任何其他答案,请告诉我。

#10


0  

Since I was building a Flash App for Facebook, I had no access to jsessionid.

自从我为Facebook构建Flash应用程序以来,我无法访问jsessionid。

I solved this problem by uploading to a HTTPS address instead of HTTP.

我通过上传到HTTPS地址而不是HTTP来解决了这个问题。

One thing that caused me trouble is that in OSX Firefox and Safari (not Chrome), the (FileReferenceInstance).type is null, and the (FileReferenceInstance).name comes with the full extension (myimage.jpg).

导致我麻烦的一件事是在OSX Firefox和Safari(不是Chrome)中,(FileReferenceInstance).type为null,而(FileReferenceInstance).name附带完整扩展名(myimage.jpg)。