.net - 在 Silverlight 中使用 HttpResponse 上传数据

标签 .net silverlight httprequest httpresponse

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

  • 我需要将我的二进制文件上传到服务器然后 >
  • 从服务器
  • 以二进制文件的形式获取答案

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

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

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

    最佳答案

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

    你需要做两件事:

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

    公共(public)类文件上传:IHttpHandler
    {
    公共(public)无效ProcessRequest(HttpContext上下文)
    {
    _httpContext = 上下文;
        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)类上传文件
    {

    公共(public)无效上传(流streamNewFile,字符串文件名,字符串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);
        }
    

    }

    关于.net - 在 Silverlight 中使用 HttpResponse 上传数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16004664/

    相关文章:

    c# - 使用 Powershell 创建 ConfigMgr 应用程序时图标质量不佳

    silverlight - 在GridValidating事件中异步调用服务

    silverlight - 在 Silverlight 中使用 HttpRequest 对象时可以设置 http header 吗?

    c# - .Net 中具有类似术语的 SQL 参数

    c# - 在 Google Drive API 中使用服务帐户下载文件时出现未经授权的响应

    c# - 具有多个具有相同值的项目时的 AutoCompleteBox 问题

    c# - 检查按钮按下是否导致任何代码被执行

    .net - WCF Web 服务和 native ASP.NET 健康监控

    go - golang-tcpclient当我发送http请求时,它总是返回(错误请求400)

    c# - 通过 POST Multipart (HTTPRequest) 发送图像