2016-05-30 12 views
2

リクエストの本文の内容をサーバー側で読み取る際に問題が発生しています。明らかに、サーバーはコンテンツタイプを持つファイルのみを読み込みます。クライアントは、ヘッダーとリクエストを送信した場合と同様:サーバがMIMEタイプなしのリクエストを読み取らない

"Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}", 
        "myFileDescription", 
        "A sample file description" 

これは、サーバー側で読まれることはありませんが、ヘッダーは次のように見える場合:

"Content-Disposition: form-data; name=\"file\";" 
     + "filename=\"temp.txt\"\r\n" 
     // Add the file's mime-type 
     + "Content-type: plain/text\r\n\r\n" 

サーバは読んでますこのヘッダーの内容私はこの問題を引き起こしているのかどうか分かりませんが、私はこの問題に関していくつかのスレッドを作成しましたが、誰も私に答えを与えてくれませんでした。

私のサーバー側のコードは次のようになります。

if (!Request.Content.IsMimeMultipartContent()) 
     { 
      throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); 
     } 

     string root = HttpContext.Current.Server.MapPath("~/App_Data"); 

     var httpRequest = HttpContext.Current.Request; 


     if (httpRequest.Files.Count > 0) 
     { 
      foreach (string file in httpRequest.Files) 
      { 

       var postedFile = httpRequest.Files[file]; 
       var filePath = HttpContext.Current.Server.MapPath("~/" + postedFile.FileName); 
       postedFile.SaveAs(filePath); 
       // NOTE: To store in memory use postedFile.InputStream 
      } 

      return Request.CreateResponse(HttpStatusCode.Created); 
     } 

     return Request.CreateResponse(HttpStatusCode.BadRequest); 
    } 

と私のクライアントコードは次のようになります。

HttpWebRequest requestToServer = (HttpWebRequest) 
WebRequest.Create(url); 

     // Define a boundary string 
     string boundaryString = "----"; 

     // Turn off the buffering of data to be written, to prevent 
     // OutOfMemoryException when sending data 
     requestToServer.AllowWriteStreamBuffering = false; 
     // Specify that request is a HTTP post 
     requestToServer.Method = WebRequestMethods.Http.Post; 
     // Specify that the content type is a multipart request 
     requestToServer.ContentType 
      = "multipart/form-data; boundary=" + boundaryString; 
     // Turn off keep alive 
     requestToServer.KeepAlive = false; 


     ASCIIEncoding ascii = new ASCIIEncoding(); 
     string boundaryStringLine = "\r\n--" + boundaryString + "\r\n"; 
     byte[] boundaryStringLineBytes = ascii.GetBytes(boundaryStringLine); 

     string lastBoundaryStringLine = "\r\n--" + boundaryString + "--\r\n"; 
     byte[] lastBoundaryStringLineBytes = ascii.GetBytes(lastBoundaryStringLine); 

     // Get the byte array of the myFileDescription content disposition 
     string myFileDescriptionContentDisposition =string.Format("Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}", 
        "myFileDescription", 
        "A sample file description"); 
     byte[] myFileDescriptionContentDispositionBytes 
      = ascii.GetBytes(myFileDescriptionContentDisposition); 

     string fileUrl = file; 
     // Get the byte array of the string part of the myFile content 
     // disposition 
     string myFileContentDisposition = string.Format(
      "Content-Disposition: form-data;name=\"{0}\"; " 
      + "filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n", 
      "myFile", Path.GetFileName(fileUrl), Path.GetExtension(fileUrl)); 
     byte[] myFileContentDispositionBytes = 
      ascii.GetBytes(myFileContentDisposition); 


     FileInfo fileInfo = new FileInfo(fileUrl); 

     // Calculate the total size of the HTTP request 
     long totalRequestBodySize = boundaryStringLineBytes.Length * 2 
      + lastBoundaryStringLineBytes.Length 
      + myFileDescriptionContentDispositionBytes.Length 
      + myFileContentDispositionBytes.Length 
      + fileInfo.Length; 
     // And indicate the value as the HTTP request content length 
     requestToServer.ContentLength = totalRequestBodySize; 


     // Write the http request body directly to the server 
     using (Stream s = requestToServer.GetRequestStreamWithTimeout(1000000)) 
     { 
      // Send the file description content disposition over to the server 
      s.Write(boundaryStringLineBytes, 0, boundaryStringLineBytes.Length); 
      s.Write(myFileDescriptionContentDispositionBytes, 0, 
       myFileDescriptionContentDispositionBytes.Length); 

      // Send the file content disposition over to the server 
      s.Write(boundaryStringLineBytes, 0, boundaryStringLineBytes.Length); 
      s.Write(myFileContentDispositionBytes, 0, 
       myFileContentDispositionBytes.Length); 

      // Send the file binaries over to the server, in 1024 bytes chunk 
      FileStream fileStream = new FileStream(fileUrl, FileMode.Open, 
       FileAccess.Read); 
      byte[] buffer = new byte[1024]; 
      int bytesRead = 0; 
      while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) 
      { 
       s.Write(buffer, 0, bytesRead); 
      } // end while 

      fileStream.Close(); 

      // Send the last part of the HTTP request body 
      s.Write(lastBoundaryStringLineBytes, 0, lastBoundaryStringLineBytes.Length); 

      WebResponse response = requestToServer.GetResponseWithTimeout(1000000); 

      StreamReader responseReader = new StreamReader(response.GetResponseStream()); 
      string replyFromServer = responseReader.ReadToEnd(); 

      return replyFromServer; 
+0

サーバーコードにブレークポイントを設定して、実行をステップ実行しましたか?問題の発生場所を特定できますか?例外がスローされますか? – gmiley

+0

そして、私は、送信されているファイルをどのように処理するかを知るために、サーバ自体にファイル名やその他のヘッダー情報を持たせる必要があると思います。 – gmiley

+0

httpRequest.Filesには、ヘッダーの内容のキーが含まれています。正しいヘッダーが入っている場合は、httpRequest.Filesに表示されます。私はファイルの内容を見るためにブレークポイントを設定しており、ファイル数は要求に含まれるファイルの数を示しています。ファイル名が必要ですか?どのような他のヘッダー情報ですか?上のコードのようにヘッダをサーバに送信したチュートリアルに続きました –

答えて

0

これは私の一部にカジュアルな間違いでした。私のサーバーサイドコードは、ファイルの内容だけを読み込みます。私のヘッダーはファイルとフォームの両方の内容を含んでいます。ファイルの内容には、Mimeタイプがサーバーで処理される必要があります。フォームデータはMIMEタイプを必要とせず、HttpContext.Current.Request.Formに含まれています。

関連する問題