Вопрос по file-upload, wcf – HttpWebRequest работает. WebClient.UploadFile не

1

Я думал, что нашел способ упростить мой кодusing WebClient.UploadFile instead of HttpWebRequest, но я получаю файл на стороне сервера, который на несколько десятков байт слишком короткий и поврежден. Есть идеи, где лежит ошибка?

Спасибо

Using HttpWebRequest (works fine):

       HttpWebRequest req = (HttpWebRequest)HttpWebRequest
                                 .Create("http://" +
                                  ConnectionManager.FileServerAddress + ":" +
                                  ConnectionManager.FileServerPort +
                                  "/binary/up/" + category + "/" +  
                                  Path.GetFileName(filename) + "/" + safehash);

        req.Method = "POST";
        req.ContentType = "binary/octet-stream";
        req.AllowWriteStreamBuffering = false;
        req.ContentLength = bytes.Length;
        Stream reqStream = req.GetRequestStream();

        int offset = 0;
        while (offset < ____)
        {
            reqStream.Write(bytes, offset, _________);
             _______
             _______
             _______

        }
        reqStream.Close();

        try
        {
            HttpWebResponse resp = (HttpWebResponse) req.GetResponse();
        }
        catch (Exception e)
        {
            _____________
        }
        return safehash;

Using WebClient (corrupt file on server end):

      var client = new WebClient();
      client.Encoding = Encoding.UTF8;
      client.Headers.Add(HttpRequestHeader.ContentType, "binary/octet-stream");

      client.UploadFile(new Uri("http://" +
              ConnectionManager.FileServerAddress + ":" +
              ConnectionManager.FileServerPort +
              "/binary/up/" + category + "/" +
              Path.GetFileName(filename) + "/" + safehash), filename);

      return safehash;

Серверная сторона - это сервис WCF:

  [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "up/file/{fileName}/{hash}")]

    void FileUpload(string fileName, string hash, Stream fileStream);
Error: User Rate Limit Exceeded carlosfigueira
Error: User Rate Limit ExceededasError: User Rate Limit ExceededWebClientError: User Rate Limit Exceeded Cᴏʀʏ
Error: User Rate Limit Exceeded Harry Mexican

Ваш Ответ

1   ответ
5

WebClient.UploadFileError: User Rate Limit ExceededWebClient.UploadDataError: User Rate Limit Exceeded

client.Encoding = Encoding.UTF8;
client.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";
byte[] fileContents = File.ReadAllBytes(filename);
client.UploadData(new Uri("http://" + ConnectionManager.FileServerAddress + ":" +
       ConnectionManager.FileServerPort +
       "/binary/up/" + category + "/" +
       Path.GetFileName(filename) + "/" + safehash), fileContents);
Error: User Rate Limit Exceeded Harry Mexican
Error: User Rate Limit Exceeded Harry Mexican

Похожие вопросы