public object Get(FileUpload request) { if (request.RelativePath.IsNullOrEmpty()) throw new ArgumentNullException("RelativePath"); var filePath = ("~/" + request.RelativePath).MapHostAbsolutePath(); if (!File.Exists(filePath)) throw new FilterInvalidBodyAccessException(request.RelativePath); var result = new HttpResult(new FileInfo(filePath)); return result; }
public object Post(FileUpload request) { if (this.RequestContext.Files.Length == 0) throw new ValidationError("UploadError", "No such file exists"); var file = this.RequestContext.Files[0]; return new FileUploadResponse { FileName = file.FileName, ContentLength = file.ContentLength, ContentType = file.ContentType, Contents = new StreamReader(file.InputStream).ReadToEnd(), }; }
public void Can_POST_upload_file_using_ServiceClient_with_request_and_QueryString() { IServiceClient client = new JsonServiceClient(Config.ServiceStackBaseUri); var uploadFile = new FileInfo("~/TestExistingDir/upload.html".MapHostAbsolutePath()); var request = new FileUpload(); var response = client.PostFileWithRequest<FileUploadResponse>( Config.ServiceStackBaseUri + "/fileuploads?CustomerId=123&CustomerName=Foo,Bar", uploadFile, request); var expectedContents = new StreamReader(uploadFile.OpenRead()).ReadToEnd(); Assert.That(response.Name, Is.EqualTo("upload")); Assert.That(response.FileName, Is.EqualTo(uploadFile.Name)); Assert.That(response.ContentLength, Is.EqualTo(uploadFile.Length)); Assert.That(response.Contents, Is.EqualTo(expectedContents)); Assert.That(response.CustomerName, Is.EqualTo("Foo,Bar")); Assert.That(response.CustomerId, Is.EqualTo(123)); }