Skip to content

Instantly share code, notes, and snippets.

@jlyonsmith
Last active February 13, 2019 17:05
Show Gist options
  • Save jlyonsmith/4bf5b5a1082e6e036466 to your computer and use it in GitHub Desktop.
Save jlyonsmith/4bf5b5a1082e6e036466 to your computer and use it in GitHub Desktop.
Using IRequiresRequestStream with ServiceStack
angular
.module('MyApp', [])
.directive("fileread", [function () {
return {
scope: {
fileread: "="
},
link: function (scope, element, attributes) {
element.bind("change", function (changeEvent) {
scope.$apply(function () {
scope.fileread = changeEvent.target.files;
});
});
}
}
}])
.controller('MyCtrl', function($scope, $http) {
var uploadFile = function(file) {
var reader = new FileReader();
chunkSize = 4096;
var totalChunks = Math.ceil(file.size / chunkSize);
var chunk = 0;
var sendNextChunk = function() {
var start, end;
start = chunk * chunkSize;
if (start > file.size) {
start = end + 1;
}
end = start + (chunkSize - 1 >= file.size ? file.size : start + chunkSize - 1);
reader.onload = function(e) {
var buffer = e.target.result;
$http({
method : "POST",
url : "http://localhost:4337/upload",
headers: {
'Content-Type': 'image/jpeg'
},
transformRequest: function(data) { return data; },
data : buffer
})
.success(function(data) {
chunk++;
if (chunk < totalChunks) {
sendNextChunk();
}
});
};
reader.readAsArrayBuffer(file.slice(start, end));
};
sendNextChunk();
}
$scope.upload = function() {
uploadFile($scope.files[0]);
}
});
<html>
<head>
<script src="angular.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-app="MyApp">
<div ng-controller="MyCtrl">
<input type="file" fileread="files" />
<br/>
<button ng-click="upload()">Upload</button>
</div>
</body>
</html>
using System;
using Mono.Unix;
using Mono.Unix.Native;
using System.Configuration;
using ServiceStack;
using ServiceStack.Web;
using System.IO;
using ServiceStack.Host.HttpListener;
using System.Net;
namespace TestService
{
public class AppHost : AppHostHttpListenerBase
{
public AppHost() : base("File Upload Service", typeof(AppHost).Assembly)
{
Plugins.Add(new ToolBelt.ServiceStack.CorsFeature(
allowOrigins: "*",
allowHeaders: ToolBelt.ServiceStack.CorsFeature.DefaultHeaders,
exposeHeaders: true,
allowCredentials: false));
}
public override void Configure(Funq.Container container)
{
SetConfig(new HostConfig { DebugMode = true });
}
}
[Route("/upload")]
public class UploadFileRequest : IRequiresRequestStream
{
public Stream RequestStream { get; set; }
}
public class UploadService : ServiceStack.Service
{
public object Any(UploadFileRequest request)
{
var dirPath = Path.Combine(Environment.CurrentDirectory, "uploads");
if (!Directory.Exists(dirPath))
Directory.CreateDirectory(dirPath);
var filePath = Path.Combine(dirPath, "puppy.jpg");
long length = 0;
using (FileStream stream = new FileStream(filePath, FileMode.Append))
{
request.RequestStream.WriteTo(stream);
length = stream.Length;
}
Console.WriteLine("File '{0}', {1} bytes uploaded", filePath, length);
return new HttpResult();
}
}
class Program
{
static void Main(string[] args)
{
var appHost = new AppHost();
appHost.Init();
appHost.Start("http://localhost:4337/");
UnixSignal[] signals = new UnixSignal[] {
new UnixSignal(Signum.SIGINT),
new UnixSignal(Signum.SIGTERM),
};
// Wait for a unix signal
for (bool exit = false; !exit;)
{
int id = UnixSignal.WaitAny(signals);
if (id >= 0 && id < signals.Length)
{
if (signals[id].IsSet)
exit = true;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment