<body>
<div>
<form id="fileUpload" action="@Url.Action("AddImage")" method="POST" enctype="multipart/form-data">
<input id="fileInput" type="file" />
<input type="button" value="Upload file" id="btnFileUpload" />
</form>
</div>
<script type="text/javascript">
$("#btnFileUpload").click(function() {
var formData = new FormData();
var file = document.getElementById("fileInput").files[0];
formData.append("fileInput", file);
$.ajax({
url: "Home/AddImage",
type: "POST",
data: formData,
contentType: false,
processData: false,
success: function() {
alert("URA");
}
});
});
</script>
</body>
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult AddImage(HttpPostedFileBase fileInput)
{
string fileName = fileInput.FileName;
var image = new Bitmap(fileInput.InputStream, false);
image.Save(Path.Combine(HttpContext.Server.MapPath("/Images/"), fileName));
return View("Index");
}
var fileE = document.getElementById("inputID");
fileE.onchange = function () {
var xhr = new XMLHttpRequest();
xhr.open("POST", "/Home/Index");
xhr.send(this.files[0]);
}
[HttpPost]
public JsonResult Index(HttpPostedFileBase fileAvatar)
{
var ggg = Request.BinaryRead(Request.ContentLength);
// Запись на диск
System.IO.File.WriteAllBytes(@"C:\1.jpg", ggg);
// или в Bitmap
var originalImage = ConvertToBitmap(ggg);
var d = originalImage;
return null;
}
private static Bitmap ConvertToBitmap(byte[] imagesSource)
{
var imageConverter = new ImageConverter();
var image = (Image)imageConverter.ConvertFrom(imagesSource);
return new Bitmap(image);
}
var fileE = document.getElementById("inputID");
fileE.onchange = function () {
var xhr = XMLHttpRequest();
var data = new FormData;
data.append("image", this.files[0]);
xhr.open("POST", "/Home/Index");
xhr.send(data);
}
[HttpPost]
public ActionResult Index(HttpPostedFileBase image)
{
var originalImage = new Bitmap(image.InputStream, false);
return null;
}