Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Test.Controllers
- {
- using System.IO;
- using System.Linq;
- using System.Web;
- using System.Web.Http;
- public class FileUploadController : ApiController
- {
- [HttpPost]
- public void UploadFile()
- {
- if (HttpContext.Current.Request.Files.AllKeys.Any())
- {
- // Get the uploaded image from the Files collection
- var httpPostedFile = HttpContext.Current.Request.Files["UploadedImage"];
- if (httpPostedFile != null)
- {
- // Validate the uploaded image(optional)
- // Get the complete file path
- var fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath("~/UploadedFiles"), httpPostedFile.FileName);
- // Save the uploaded file to "UploadedFiles" folder
- httpPostedFile.SaveAs(fileSavePath);
- }
- }
- }
- }
- }
- <form id="register-user">
- <label for="fileUpload">
- Select File to Upload:
- </label>
- <input id="fileUpload" type="file" />
- <input id="btnUploadFile" type="submit" value="Upload File" />
- </form>
- <script type="text/javascript">
- $(document).ready(function () {
- $('#register-user').submit(function () {
- console.log('in submit')
- var data = new FormData();
- var files = $("#fileUpload").get(0).files;
- // Add the uploaded image content to the form data collection
- if (files.length > 0) {
- data.append("UploadedImage", files[0]);
- }
- // Make Ajax request with the contentType = false, and procesDate = false
- var ajaxRequest = $.ajax({
- type: "POST",
- url: "http://localhost:63362/api/fileupload/uploadfile",
- contentType: false,
- processData: false,
- data: data
- });
- ajaxRequest.done(function (xhr, textStatus) {
- // Do other operation
- });
- });
- });
- </script>
- //http://www.codeproject.com/Articles/806075/File-Upload-using-jQuery-AJAX-in-ASP-NET-Web-API
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement