Search Unity

500 internal Server error : While Uploading File With WWW and WWWForm

Discussion in 'Editor & General Support' started by Y76_Unity, Aug 18, 2018.

  1. Y76_Unity

    Y76_Unity

    Joined:
    Jan 29, 2018
    Posts:
    28
    I am trying to upload a file to the server using www and wwwForm from the client Side using this code :

    Code (CSharp):
    1. WWW localFile = new WWW("file://"+ FilePath);
    2.         yield return localFile;
    3.  
    4.         WWWForm postForm = new WWWForm();
    5.         postForm.AddField("frameCount", Time.frameCount.ToString());
    6.         postForm.AddBinaryData("file", localFile.bytes, "filename.obj");
    7.  
    8.         WWW upload = new WWW(uploadUrl, postForm);
    9.         yield return upload;
    10.  
    11.         if (upload.error == null)
    12.         {
    13.             Debug.Log(upload.text);
    14.         }
    15.         else
    16.         {
    17.             Debug.Log("Error during upload: " + upload.error);
    18.         }
    Calling the ASP.net Service Upload File :
    Code (CSharp):
    1. public async Task<HttpResponseMessage> UploadFileAsync()
    2.         {
    3.             try
    4.             {
    5.                 var provider = new MultipartMemoryStreamProvider();
    6.                 await Request.Content.ReadAsMultipartAsync(provider);
    7.                 foreach (var file in provider.Contents)
    8.                 {
    9.                     var dataStream = await file.ReadAsStreamAsync();
    10.                     // use the data stream to persist the data to the server (file system etc)
    11.                     Stream fileStream = File.Create("D:\\filename.obj");
    12.                     dataStream.CopyTo(fileStream);
    13.                     fileStream.Close();
    14.                     dataStream.Close();
    15.                     var response = Request.CreateResponse(HttpStatusCode.OK);
    16.                     response.Content = new StringContent("Successful upload", Encoding.UTF8, "text/plain");
    17.                     response.Content.Headers.ContentType = new MediaTypeWithQualityHeaderValue(@"text/html");
    18.                     return response;
    19.                 }
    20.                 return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No File Uploaded");
    21.             }
    22.             catch (Exception e)
    23.             {
    24.                 return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e.Message);
    25.             }
    26.         }
    and I am getting ( 500 Internal Server Error)?
    the service was running Perfectly, but something wrong Happen, I get 500 Internal Server Error
    So what is the Wrong with That?
    Thanks in Advance ..