I am POSTING to a web service (not SS) which wants the data to look like this:
------WebKitFormBoundaryiWW3t1uvuxBu0zpb
Content-Disposition: form-data; name=“portalId”
0119821
------WebKitFormBoundaryiWW3t1uvuxBu0zpb
Content-Disposition: form-data; name=“logo” ; filename=“New Logo.jpg”
Content-Type: image/jpeg
I am trying to use PostFileWithRequest like this:
var asdas = new PortalImageRequest();
asdas.PortalId = txtEliteNumber.Text;
var files = new FileStream(“logo.jpg”,FileMode.Open);
var asdas1 = jsClient.PostFileWithRequest(“https://thesite.com/portalImages",files,"logo.jpg ”, asdas,“logo”);
but this always results in this, according to fiddler:
–b4e1e595-2728-4cef-843a-3a19b06a0bbf
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=“portalId”
1941913
–b4e1e595-2728-4cef-843a-3a19b06a0bbf
Content-Disposition: form-data; name=file ; filename=logo.jpg
Content-Type: image/jpeg
Note that the name does not appear to be set properly based on what i see here:
// Copyright (c) ServiceStack, Inc. All Rights Reserved.
// License: https://raw.github.com/ServiceStack/ServiceStack/master/license.txt
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Threading;
using ServiceStack.Logging;
using ServiceStack.Messaging;
using ServiceStack.Text;
using ServiceStack.Web;
namespace ServiceStack
{
This file has been truncated. show original
i tried with new FileInfo(“logo.jpg”) as well but same result
so, how can i change the name of the field being sent? is there a way to do the same kind of thing witth httputils?
thanks!
mythz
July 9, 2020, 4:37pm
2
Note: The Service Clients are only meant for calling ServiceStack Services but the PostFileWithRequest
APIs does have an optional fieldName
param in their APIs:
public virtual TResponse PostFileWithRequest<TResponse>(Stream fileToUpload, string fileName, object request, string fieldName = "upload")
{
return PostFileWithRequest<TResponse>(ResolveTypedUrl(HttpMethods.Post, request), fileToUpload, fileName, request, fieldName);
}
public virtual TResponse PostFileWithRequest<TResponse>(string relativeOrAbsoluteUrl, Stream fileToUpload, string fileName, object request, string fieldName = "upload")
right, and i am using that, but it is NOT setting the fieldName as expected.
jsClient.PostFileWithRequest(“https://thesite.com/portalImages",files,"logo.jpg ”, asdas,“logo”) ;
this is what i am asking about, because from the code, it should be flipping fieldName to “logo” in my case, but it is not
i tried BOTH stream and fileinfo approaches too.
any ideas?
mythz
July 9, 2020, 5:05pm
4
We have integration tests showing it working in FileUploadTests.cs , I don’t know what the issue with the 3rd Party API is, it’s also not clear why the params are not quoted any where the Content-Type came from, i.e:
Content-Disposition: form-data; name=file; filename=logo.jpg
Content-Type: image/jpeg
As the file is written with quoted params & without a File Content-Type:
outputStream.Write(boundary + newLine);
outputStream.Write($"Content-Disposition: form-data;name=\"{fieldName}\";filename=\"{fileName}\"{newLine}{newLine}");
var buffer = new byte[4096];
int byteCount;
int bytesWritten = 0;
while ((byteCount = fileToUpload.Read(buffer, 0, 4096)) > 0)
{
outputStream.Write(buffer, 0, byteCount);
if (OnUploadProgress != null)
{
bytesWritten += byteCount;
OnUploadProgress(bytesWritten, fileToUpload.Length);
}
}
outputStream.Write(newLine);
outputStream.Write(boundary + "--");
this is a goofy API for sure. ill see if i can find a workaround, but wanted to use SS as you make it so easy!