It has been some time since I messed with this code -- it was a personal project that I haven't been back to for awhile and not at all refined, but I just tested it again and it is working.
//JS//
I'm using drag and drop in the UI so in my drop event I have:
var files = e.originalEvent.dataTransfer.files;
instance._uploadFile(files[0]);
And then:
_uploadFile: function (file) {
var instance = this;
var xhr = new XMLHttpRequest();
if (xhr.upload && file.size <= instance.options.maxFileSize) {
xhr.upload.addEventListener("progress", function (e) {
var progress = parseInt(100 - (e.loaded / e.total * 100));
instance.element.find('.progress').css({ width: progress + '%' }).show();
}, false);
xhr.onreadystatechange = function (e) {
if (xhr.readyState == 4) {
instance.element.find('.progress').addClass((xhr.status == 200 ? "success" : "failure"));
}
};
var S = $.ServicesFramework(instance.options.moduleId);
var url = S.getServiceRoot('VoxDynamic.OM') + "/AudioFile/Upload";
var formData = new FormData();
formData.append("file", file);
xhr.open("POST", url, true);
xhr.setRequestHeader("X_PORTALID", instance.options.portalId);
xhr.send(formData);
}
}
//AudioFileController.cs//
And for the web service:
public class AudioFileController : DnnApiController
{
[AllowAnonymous]
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<HttpResponseMessage> Upload()
{
var portalId = Convert.ToInt32(Request.Headers.Where(x=> x.Key == "X_PORTALID").Single().Value.First());
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType));
}
var provider = new FilteredMultipartMemoryStreamProvider(new string[] { "mp3" });
await Request.Content.ReadAsMultipartAsync(provider);
if (provider.Contents.Count > 0)
{
var content = provider.Contents.Single();
var stream = await content.ReadAsStreamAsync();
IFileInfo tempFile;
if (!FolderManager.Instance.FolderExists(portalId, "Audio"))
{
CreateAudioFolder(portalId);
}
var audioFolder = FolderManager.Instance.GetFolder(portalId, "Audio");
if (stream.Length != 0)
{
tempFile = FileManager.Instance.AddFile(audioFolder, Guid.NewGuid().ToString() + ".mp3", stream);
return Request.CreateResponse(HttpStatusCode.OK, tempFile.FileId);
}
}
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType, "Count < 1"));
}
}
public class FilteredMultipartMemoryStreamProvider : MultipartMemoryStreamProvider
{
private string[] _extensions;
public FilteredMultipartMemoryStreamProvider(string[] extensions)
{
_extensions = extensions;
}
public override System.IO.Stream GetStream(HttpContent parent, System.Net.Http.Headers.HttpContentHeaders headers)
{
var filename = headers.ContentDisposition.FileName.Replace("\"", string.Empty);
if (filename.IndexOf('.') < 0)
return Stream.Null;
var extension = filename.Split('.').Last();
return _extensions.Any(i => i.Equals(extension, StringComparison.InvariantCultureIgnoreCase))
? base.GetStream(parent, headers)
: Stream.Null;
}
}