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', ['angularFileUpload'])
.controller('MyCtrl', function($scope, $upload) {
$scope.onFileSelect = function($files) {
for (var i = 0; i < $files.length; i++) {
var file = $files[i];
$scope.upload = $upload.upload({
url: 'http://localhost:4337/upload',
file: file
})
.progress(function(e) {
console.log('percent: ' + parseInt(100.0 * e.loaded / e.total));
})
.success(function(data, status, headers, config) {
console.log(data);
});
}
}
});
<html>
<head>
<script src="angular-file-upload-html5-shim.min.js"></script>
<script src="angular.min.js"></script>
<script src="angular-file-upload.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-app="MyApp">
<div ng-controller="MyCtrl">
<input type="file" ng-file-select="onFileSelect($files)"/>
<br/>
<button ng-click="upload.abort()">Cancel 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 fileId = Guid.NewGuid();
var filePath = Path.Combine(dirPath, fileId.ToString("N"));
if (File.Exists(filePath))
File.Delete(filePath);
long length = 0;
using (FileStream stream = File.Create(filePath))
{
request.RequestStream.WriteTo(stream);
length = stream.Length;
}
Console.WriteLine("File '{0}' ({1} bytes) has been uploaded", filePath, length);
return new { UploadId = fileId.ToString("N") }.ToJson();
}
}
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