在flex中流式传输webservice

时间:2013-01-15 23:11:30

标签: flex soap actionscript upload urlloader

我正在尝试在flex / air应用程序中创建文件上载,该应用程序将信息发送到.NET wcf SOAP webservice。文件上载还必须具有指示事件的进度。该服务使用Stream作为MessageBodyMember来允许流式上传。我的输入看起来有点像这样:

[MessageContract]
public class SendFileRequestMessage : IDisposable
{
    public string UserName;

    [MessageBodyMember(Order = 1)]
    public Stream FileData;
}

,服务看起来像这样:

public interface IFileTransferService
{
    [OperationContract]
    SendFileResponseMessage SendFile(SendFileRequestMessage request);
}

现在,当我在VS2010中创建代理类时,我获得了FileData的Stream对象。如果我在Flash Builder 4.7中执行相同操作,则FileData将被解释为ByteArray。我已经在我的客户端查看了FileUpload和UrlLoader,但我无法设置正文成员。我的动作脚本现在看起来像这样 无法正常工作

var dataToSave:XML = <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:q0="http://mystuff/uploadservice/v1"><soapenv:Body><q0:SendFile/></soapenv:Body></soapenv:Envelope>

            var request:URLRequest = new URLRequest("http://localhost:31454/Uploadhandler.svc");
            request.requestHeaders = new Array(new URLRequestHeader("SOAPAction", "http://mystuff/uploadservice/v1/IFileTransferService/SendFile"));
            request.data = dataToSave;
            request.contentType = "text/xml; charset=utf-8";

            request.method = URLRequestMethod.POST;

            var loader:URLLoader = new URLLoader();
            loader.load(request);

那么如何从flex上传到soap服务的流媒体文件呢?任何帮助都将非常感激。

1 个答案:

答案 0 :(得分:0)

我不熟悉.NET SOAP模型,但如果它需要文件数据的标准http内容处置,您可以尝试获取文件的FileReference对象,然后在其upload方法中传递URLRequiest。在你的情况下,这可能就像

        ... Create class level variable ...
        var fr:FileReference = new FileReference();

        ... Obtain file reference somewhere ...
        //Set handlers for Filereference events 
        fr.browse(); //Obtain actual file reference  


        ... Somewhere in selectHandler chain....

        var dataToSave:XML = <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:q0="http://mystuff/uploadservice/v1"><soapenv:Body><q0:SendFile/></soapenv:Body></soapenv:Envelope>

        var request:URLRequest = new URLRequest("http://localhost:31454/Uploadhandler.svc");
        request.requestHeaders = new Array(new URLRequestHeader("SOAPAction", "http://mystuff/uploadservice/v1/IFileTransferService/SendFile"));
        request.data = dataToSave;
        request.contentType = "text/xml; charset=utf-8";
        request.method = URLRequestMethod.POST;

        //Do POST
        fr.upload(request);

Documentation包含使用FileReference的示例。