4

如何在 Silverlight 中使用异步双向二进制上传和下载数据?

  1. 我需要将我的二进制文件上传到服务器,然后 >
  2. 从服务器获取答案也作为二进制文件

我当前使用的示例是:http: //msdn.microsoft.com/en-us/library/system.net.webrequest.begingetresponse.aspx

但是示例以请求开始,我需要从上传开始。

我已经尝试过使用 WebClient,但我无法按一个顺序进行上传/下载:Bidirectional use of Webclient for binary

4

2 回答 2

2

我所做的是由 Silverlight 中的 Handlers.(ASHX) 同步的。RadFileUploader 是 Telerik 的组件与这些处理程序一起使用。

  namespace MyNameSpace.Web
    {   
      public class FileHandler : IHttpHandler
           {

      public void ProcessRequest(HttpContext context)
      {

        if (context.User.Identity.IsAuthenticated)
        {

            string fileName = context.Request.QueryString.Get("fileName");

            if (File.Exists(fileName))
            {                 
                context.Response.ContentType = MimeType(fileName);
                context.Response.AddHeader("Content-Transfer-Encoding", "binary");
                context.Response.WriteFile(fileName, 0, length);
            }
          }
   }

我削减了大部分代码,但它给出了这个想法。我使用了一个处理程序来播放音频文件和一个处理程序来下载音频文件。Mimetype 是非常重要的部分。你的标题也很重要。通过这种方式,你告诉你想做什么。

对于异步,此页面可能会有所帮助。http://msdn.microsoft.com/en-us/library/ms227433(v=vs.100).aspx

编辑

下面的处理程序文件"Top.jasper"从目录中读取文件并将其写入回调方法。(ar.IsCompleted)参数的 IsCompleted 属性检查其是否已完成。

<%@ WebHandler Language="C#" CodeBehind="BDTest.ashx.cs" Class="AHBSBus.Web.Classes.BDTest" %>

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Threading;
     using System.IO;

    namespace AHBSBus.Web.Classes
     {
/// <summary>
/// Summary description for BDTest
/// </summary>
public class BDTest : IHttpAsyncHandler
{
    public bool IsReusable { get { return false; } }


    public BDTest()
    {

    }
    public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
    {
        cb = new AsyncCallback((ar) =>
            {

                if (ar.IsCompleted)
                {
                    var result = ar.AsyncState;

                    File.WriteAllBytes("c:\\new.jasper", (byte[])result);
                }
            });

        extraData = File.ReadAllBytes("c:\\Top.jasper");

        context.Response.Write("<p>Begin IsThreadPoolThread is " + Thread.CurrentThread.IsThreadPoolThread + "</p>\r\n");
        AsynchOperation asynch = new AsynchOperation(cb, context, extraData);
        asynch.StartAsyncWork();
        return asynch;
    }

    public void EndProcessRequest(IAsyncResult result)
    {
    }

    public void ProcessRequest(HttpContext context)
    {
        var ctx=context;
    }
}

class AsynchOperation : IAsyncResult
{
    private bool _completed;
    private Object _state;
    private AsyncCallback _callback;
    private HttpContext _context;

    bool IAsyncResult.IsCompleted { get { return _completed; } }
    WaitHandle IAsyncResult.AsyncWaitHandle { get { return null; } }
    Object IAsyncResult.AsyncState { get { return _state; } }
    bool IAsyncResult.CompletedSynchronously { get { return false; } }

    public AsynchOperation(AsyncCallback callback, HttpContext context, Object state)
    {
        _callback = callback;
        _context = context;
        _state = state;
        _completed = false;
    }

    public void StartAsyncWork()
    {
        ThreadPool.QueueUserWorkItem(new WaitCallback(StartAsyncTask), null);
    }

    private void StartAsyncTask(Object workItemState)
    {
        //You may modify _state object here            
        _context.Response.Write("<p>Completion IsThreadPoolThread is " + Thread.CurrentThread.IsThreadPoolThread + "</p>\r\n");

        _context.Response.Write("Hello World from Async Handler!");
        _completed = true;
        _callback(this);
    }
  }
 }

我提供SignalR双向通信。

于 2013-04-15T05:57:54.533 回答
2

AHSX 是一种方法,但要特别考虑超过 4 MB 的文件。

你需要做两件事:

  • 服务器端:将处理接收到的数据的 Ashx HttpHandler:

    公共类 FileUpload : IHttpHandler { public void ProcessRequest(HttpContext context) { _httpContext = context;

        if (context.Request.InputStream.Length == 0)
            throw new ArgumentException("No file input");
    
        try
        {
            // Method that will be populating parameters from HttpHandler Request.
            GetQueryStringParameters();
    
            string uploadFolder = _directory;
            string tempFileName = _fileName + _tempExtension;
    
            if (_firstChunk)
            {
                //// Delete temp file
                if (File.Exists(@HostingEnvironment.ApplicationPhysicalPath + "/" + uploadFolder + "/" + tempFileName))
                    File.Delete(@HostingEnvironment.ApplicationPhysicalPath + "/" + uploadFolder + "/" + tempFileName);
    
                //// Delete target file
                if (File.Exists(@HostingEnvironment.ApplicationPhysicalPath + "/" + uploadFolder + "/" + _fileName))
                    File.Delete(@HostingEnvironment.ApplicationPhysicalPath + "/" + uploadFolder + "/" + _fileName);
    
            }
    
    
    
            using (FileStream fs = File.Open(@HostingEnvironment.ApplicationPhysicalPath + "/" + uploadFolder + "/" + tempFileName, FileMode.Append))
            {
                SaveFile(context.Request.InputStream, fs);
                fs.Close();
            }
    
            if (_lastChunk)
            {
                File.Move(HostingEnvironment.ApplicationPhysicalPath + "/" + uploadFolder + "/" + tempFileName, HostingEnvironment.ApplicationPhysicalPath + "/" + uploadFolder + "/" + _fileName);
            }
    
    
        }
        catch (Exception e)
        {
            // Todo
            // Logger Exception
            throw;
        }
    
    }
    

    }

  • 将调用它的 Silverlight 类:

公共类上传文件 {

public void Upload(Stream streamNewFile, string fileName, string dirOnServer = "") { this.uploadMode = UploadMode.OpenStream;

        this.destinationOnServer = dirOnServer;
        this.fileStream = streamNewFile;
        this.fileName = fileName;
        this.dataLength = this.fileStream.Length;

        long dataToSend = this.dataLength - this.dataSent;
        bool isLastChunk = dataToSend <= this.chunkSize;
        bool isFirstChunk = this.dataSent == 0;
        string docType = "document";

        var strCurrentHost = HtmlPage.Document.DocumentUri.ToString().Substring(0, HtmlPage.Document.DocumentUri.ToString().LastIndexOf('/'));
        UriBuilder httpHandlerUrlBuilder = new UriBuilder(strCurrentHost + "/FileUpload.ashx");
        httpHandlerUrlBuilder.Query = string.Format("{5}file={0}&offset={1}&last={2}&first={3}&docType={4}&destination={6}",
            fileName, this.dataSent, isLastChunk, isFirstChunk, docType,
            string.IsNullOrEmpty(httpHandlerUrlBuilder.Query) ? string.Empty : httpHandlerUrlBuilder.Query.Remove(0, 1) + "&", destinationOnServer);

        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(httpHandlerUrlBuilder.Uri);
        webRequest.Method = "POST";
        webRequest.BeginGetRequestStream(new AsyncCallback(this.WriteToStreamCallback), webRequest);
    }

}

于 2013-04-25T14:18:35.517 回答