Good afternoon.
I need to use the IRequiresRequestStream interface to transfer huge files from my front end (WPF Application) to my Service using a PCL DLL. My goal is to create a base class, let’s call it TransferProxy who is in charge of the creation of the HttpWebRequest and execute all the stuff.
string filePath = @"D:\MyFile.zip";
HttpWebRequest client =(HttpWebRequest)WebRequest.Create(@"http://localhost:9056/upload/myfile.zip");
client.Method = WebRequestMethods.Http.Post;
// the following 4 rows enable streaming
client.AllowWriteStreamBuffering = false;
client.SendChunked = true;
client.ContentType = "multipart/form-data;";
client.Timeout = int.MaxValue;
using (FileStream fileStream = File.OpenRead(filePath))
{
fileStream.Copy(client.GetRequestStream());
}
where “Copy(client.GetRequestStream());” is
public static class StreamExtender
{
public static void Copy(this Stream instance, Stream target)
{
int bytesRead = 0;
int bufSize = copyBuf.Length;
while ((bytesRead = instance.Read(copyBuf, 0, bufSize)) > 0)
{
target.Write(copyBuf, 0, bytesRead);
}
}
private static readonly byte[] copyBuf = new byte[0x1000];
}
The server side code is the following
public class UploadService : ServiceStack.Service
{
public object Post(UploadPackage request)
{
// hack - get the properties from the request
if (string.IsNullOrEmpty(request.FileName))
{
var segments = base.Request.PathInfo.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
request.FileName = segments[1];
}
string resultFile = Path.Combine(@"C:\Temp", request.FileName);
if (File.Exists(resultFile))
{
File.Delete(resultFile);
}
using (FileStream file = File.Create(resultFile))
{
request.RequestStream.Copy(file);
}
return new HttpResult(System.Net.HttpStatusCode.OK);
}
}
The route is
[Route("/upload/{FileName}", "POST")]
public class UploadPackage : IRequiresRequestStream
{
public System.IO.Stream RequestStream { get; set; }
public string FileName { get; set; }
}
This code works like a charm if the TransferProxy is inside a Class Library, but when I try to move it in a Portable Class Library some properties of the HttpWebRequest are missing and, obviously, the FileStream is missing. Can you please give some tips on how I can implement what I need (if possible…)?
Thanks
Enrico