Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. var provider = new MultipartMemoryStreamProvider();
  2. await Request.Content.ReadAsMultipartAsync(provider);
  3.  
  4. foreach (var file in provider.Contents)
  5. {
  6. var filename = file.Headers.ContentDisposition.FileName.Trim('"');
  7. var buffer = await file.ReadAsByteArrayAsync();
  8. // Doing stuff with the file here
  9. }
  10.  
  11. $apiKey=$args[0]
  12. Write-Host "Using API Key: $apiKey"
  13. $fileId=$args[1]
  14. Write-Host "Using File ID: $fileId"
  15. $sourceFile=$args[2]
  16. Write-Host "Using File: $sourceFile"
  17.  
  18. $baseUrl = "http://localhost:23857/api"
  19. $url = "$baseUrl/file/$fileId/version"
  20.  
  21. $fileBin = [IO.File]::ReadAllBytes($sourceFile)
  22. $computer= $env:COMPUTERNAME
  23. $enc = [System.Text.Encoding]::GetEncoding("iso-8859-1")
  24. $fileEnc = $enc.GetString($fileBin)
  25. $boundary = [System.Guid]::NewGuid().ToString()
  26.  
  27. $LF = "`n"
  28. $bodyLines = (
  29. "--$boundary",
  30. "Content-Disposition: form-data; name=`"filename.txt`"$LF", # filename= is optional
  31. $fileEnc,
  32. "--$boundary",
  33. "Content-Disposition: form-data; name=`"computer`"$LF",
  34. $computer,
  35. "--$boundary--$LF"
  36. ) -join $LF
  37.  
  38. try {
  39. # Returns the response gotten from the server (we pass it on).
  40. #
  41. Invoke-RestMethod -Uri $url -Method Post -ContentType "multipart/form-data; boundary=`"$boundary`"" -TimeoutSec 120 -Body $bodyLines -Headers @{"ApiKey"=$apiKey}
  42. }
  43. catch [System.Net.WebException] {
  44. Write-Error( "FAILED to reach '$URL': $_" )
  45. throw $_
  46. }
  47.  
  48. Unexpected end of MIME multipart stream. MIME multipart message is not complete.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement