<div>
<input type="file" id="file"/>
<button ng-click="uploadClick()">Upload</button>
</div>
angular.module('app')
.controller('AdminCtrl', ['$scope', '$http',
function ($scope, $http) {
$scope.uploadClick = function () {
var file = document.getElementById('file').files[0];
var formData = new FormData();
formData.append('file', file);
$http.post('/api/images', formData, {
transformRequest: angular.identity,
headers: { 'Content-Type': undefined }
}).then(
function (result) {
console.log('Success.', result);
},
function (result) {
console.log('Failed to upload.', result);
});
};
}
]);
[RoutePrefix("api/images")]
public class ImagesController : ApiController
{
[Route()]
public IHttpActionResult PostImage()
{
var o = Request.Content.ReadAsMultipartAsync().Result;
using (var s = o.Contents[0].ReadAsStreamAsync().Result)
using(var image = Image.FromStream(s))
image.Save(@"c:\temp\__test.jpg");
return Ok(new PostImageResult { Id = 0 });
}
}
public class PostImageResult
{
public int Id { get; set; }
}
Wednesday, November 26, 2014
Wednesday, February 17, 2010
DropDownList.SelectItemByValue Extension Method (C#)
This helped me simplify some code today. Basically it is an extension method for a DropDownList that takes a nullable type value as an argument to find in the list. If it finds the value it becomes selected, otherwise it selects the first item by default.
In a static class:
In a static class:
public static void SelectItemByValue(this DropDownList dropDownList, Nullable value) where T : struct
{
if (value.HasValue)
{
ListItem listItem = dropDownList.Items.FindByValue(value.Value.ToString());
if (listItem != null)
{
listItem.Selected = true;
return;
}
}
if (dropDownList.Items.Count > 0)
{
dropDownList.Items[0].Selected = true;
return;
}
}
Subscribe to:
Posts (Atom)