3
Guys the question is this, I have a <input type="file" id="fileupload"/>
and a field <input type="text id="Nome"/>
. To pass it to my controller, I do the following:
var data = $("#fileupload").get(0).files[0];
var formdata = new FormData();
formdata.append("Files", data);
var xhr = new XMLHttpRequest();
xhr.open("POST", "/Home/UploadFiles", true);
xhr.send(formdata);
so far so good, the problem is that I wanted, along with the file, to pass the name inputado by the user in the element "Name". There on the other side I would save you with the new name (keeping the extension) somewhere. I tried everything and I couldn’t get the name... How can I do?
Thanks in advance!
Note: Gypsy Morrison, my controller is this:
[HttpPost]
public ContentResult UploadFiles()
{
//Pego o arquivo
HttpPostedFileBase hpf = Request.Files[0] as HttpPostedFileBase;
//Pego a extensao dele para selecionar o tipo
string extensao = hpf.FileName.Substring(hpf.FileName.Length - 4).ToLower();
//Seleciono o tipo de arquivo e retorno se não for o que eu quero
if (!(extensao == ".jpg" || extensao == ".gif" || extensao == ".png" || extensao == ".pdf"))
{
return Content("{\"success\":\"" + false + "\",\"message\":\"" + "Estensão não permitida!" + "" + "\"}", "application/json");
}
//Configuro o nome do arquivo para salvá-lo (nessa parte eu realmente queria o nome passado pelo usuário)
string fileName = hpf.FileName.Replace(" ","").Replace(".","").Replace("'","").ToUpper() + "-" + String.Format("{0:yyyyMMddhhmmss}", DateTime.Now) + extensao;
//Salvo o arquivo
string savedFileName = Path.Combine(Server.MapPath("~/Empresa"), fileName);
hpf.SaveAs(savedFileName);
return Content("{\"success\":\"" + true + "\",\"message\":\"" + "Upload finalizado com sucesso!" + "" + "\"}", "application/json");
}
You can put the method of your Controller that receives this request?
– Leonel Sanches da Silva