I have a controller link to an input element for uploading. In my controller I am getting a weird error that I do not quite understand. Severity Code Description Project Path File Line Suppression State
Error CS1061 'HttpRequestMessage' does not contain a definition for 'Files' and no accessible extension method 'Files' accepting a first argument of type 'HttpRequestMessage' could be found (are you missing a using directive or an assembly reference?) SimSentinel C:\Users\tsach\Source\Workspaces\SIMSentinelv2\Website\SimSentinel\SimSentinel\Controllers C:\Users\tsach\Source\Workspaces\SIMSentinelv2\Website\SimSentinel\SimSentinel\Controllers\BulkSMSUploadController.cs
using System.Data;
using System.Linq;
using System.Web.Http;
using System.Web.Security;
using Repositories.Interfaces;
using Repositories.Interfaces.Dtos;
using SimSentinel.Models;
using System;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
//using System.Web.Http.HttpPut;
namespace SimSentinel.Controllers
{
[System.Web.Http.Authorize]
public class BulkSMSUploadController : ApiController
{
public ActionResult Index()
{
//ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
//return View();
return null;
}
[System.Web.Mvc.HttpPost]
public ActionResult UploadFiles()
{
if (Request.Files.Count <= 0)
{
return Json("No files selected.");
}
else
{
try
{
HttpFileCollectionBase files = Request.Files;
for (int i = 0; i < files.Count; i++)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "Uploads/";
string filename = Path.GetFileName(Request.Files[i].FileName);
HttpPostedFileBase file = files[i];
string fname;
if (Request.Browser.Browser.ToUpper() == "IE" || Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER")
{
string[] testfiles = file.FileName.Split(new char[] { '\\' });
fname = testfiles[testfiles.Length - 1];
}
else
{
fname = file.FileName;
}
fname = Path.Combine(Server.MapPath("~/Uploads/"), fname);
file.SaveAs(fname);
}
return Json("File Uploaded Successfully!");
}
catch (Exception ex)
{
return Json("Error occurred. Error details: " + ex.Message);
}
}
}
//public ActionResult About()
//{
// ViewBag.Message = "Your app description page.";
// return View();
//}
}
}
So after all this I have adjusted my controller. See code below which works however it redirects to the actual controller and that is a problem in a SPA application. In addition, the file also saves in a wierd format almost like a randomly generated string like BodyPart_2ea18b56-0c11-41f6-81ff-204bb377cbbf
using System.Diagnostics;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
public class Upload2Controller : ApiController
{
public async Task<HttpResponseMessage> PostFormData()
{
// Check if the request contains multipart/form-data.
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string root = HttpContext.Current.Server.MapPath("~/Files");
var provider = new MultipartFormDataStreamProvider(root);
try
{
// Read the form data.
await Request.Content.ReadAsMultipartAsync(provider);
// This illustrates how to get the file names.
foreach (MultipartFileData file in provider.FileData)
{
Trace.WriteLine(file.Headers.ContentDisposition.FileName);
Trace.WriteLine("Server file path: " + file.LocalFileName);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (System.Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
}
}
}