gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 01:09:47 24 4
gpt4 key购买 nike

如何在 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/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com