I am trying to upload a single .csv file in ASP.NET MVC. In my .ascx file, I have:
<div>
<input type="file" name="file" id="file" />
   
<input type="submit" name="btnSubmit" id="btnSubmit" value="Upload" />
</div>
The controller action is:
public ActionResult Upload(HttpPostedFileBase file)
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
return View();
}
The problem is that I always get file as Null in the Upload Action. Any suggestions on how to get this working?
Are you sure you have a
<form enctype="multipart/form-data" method="post">
<div> bla bla
</div>
</form>
?
Edit :
method="post"
+
[HttpPost]
on your action
Related
I have following data in html table and trying to submit the data but I miss additional data from view to controller and I can only getting uploaded files details. how to pass file details and other data in each row simultaneously. Here I need to pass StudentName1 & StudentName2 along with corresponding file to controller.
<form action="" method="post" enctype="multipart/form-data">
<div class='row'>
<label for="txt">StudentName1:</label>
<input type="text" name="name1" id="name1" />
<label for="file1">Filename:</label>
<input type="file" name="files" id="file1" />
</div>
<div class='row'>
<label for="txt">StudentName2:</label>
<input type="text" name="name2" id="name2" />
<label for="file2">Filename:</label>
<input type="file" name="files" id="file2" />
</div>
<input type="submit" />
</form>
COntroller
[HttpPost]
public ActionResult Index(IEnumerable<HttpPostedFileBase> files) {
foreach (var file in files) {
if (file.ContentLength > 0) {
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/Myuploads"), fileName);
file.SaveAs(path);
}
}
return RedirectToAction("Index");
}
I am looking for a solution on how to send a file using a form to an asp application. I am writing in the asp .net framework MVC Razor.
My form:
<div class="container">
<div class="col-md-2">
#using (Html.BeginForm("Data", "Admin", FormMethod.Post, new { encrypte = "multipart/form-data"}))
{
<div class="form login-form">
<label for="username" class="text-info">Wczytaj plik:</label>
<input type="file" name="file" id="file" />
</div>
<div id="register-link" class="text-right">
<input type="submit" class="btn btn-success" value="Importuj" />
</div>
#ViewBag.Message
}
</div>
</div>
My controller:
[HttpPost]
public ViewResult Data(HttpPostedFile file = null)
{
if(file != null && file.ContentLength > 0)
{
string path = Path.Combine(Server.MapPath("~/Upload/Data"), Path.GetFileName(file.FileName));
file.SaveAs(path);
ViewBag.Message = "Succes";
}
return View("AdminDataView", students);
}
Unfortunately, the above code does not work, am I doing something wrong with it, is there another option to upload the file to asp?
I think there is a typo and i suggest you that use ActionResult instead of ViewResult.
Defference between ActionResult & ViewResult
the typo is: new { enctype="multipart/form-data"}) not new { encrypte = "multipart/form-data"})
Sample Code:
View
#using(Html.BeginForm("UploadFile","Upload", FormMethod.Post, new {
enctype="multipart/form-data"}))
{
<div>
#Html.TextBox("file", "", new { type= "file"}) <br />
<input type="submit" value="Upload" />
#ViewBag.Message
</div>
}
Controller
[HttpPost]
publicActionResultUploadFile(HttpPostedFileBase file)
{
try
{
if (file.ContentLength > 0)
{
string _FileName = Path.GetFileName(file.FileName);
string _path = Path.Combine(Server.MapPath("~/UploadedFiles"), _FileName);
file.SaveAs(_path);
}
ViewBag.Message = "File Uploaded Successfully!!";
return View();
}
catch
{
ViewBag.Message = "File upload failed!!";
return View();
}
}
Please refer below link this gives you better understanding.
https://stackoverflow.com/a/60519052/7761461
Hope this will surely help you.
I have a very basic multiple file uploader that works on Firefox and IE but on Chrome it hangs when I include the name="files" attribute in the input tag below.
Has anyone else experienced this problem? and if so is there a work around?
<form action="/AdminFileUpload/PostMultiple" method="post" enctype="multipart/form-data">
<input id="fileupload" type="file" name="files" multiple="multiple" />
<input type="submit" name="submit" value="Upload" />
<hr />
<b>Live Preview</b>
<br />
<br />
<div id="dvPreview">
</div>
</form>
This is the controller I am using;
If I remove name="files" from the input tag the IEnumerable<HttpPostedFileBase> files parameter in the controller is null.
[HttpPost]
public ActionResult PostMultiple(IEnumerable<HttpPostedFileBase> files)
{
foreach (var file in files)
{
if (file != null && file.ContentLength > 0)
{
file.SaveAs(Path.Combine(Server.MapPath("/Uploads"), Path.GetFileName(file.FileName)));
}
}
return RedirectToAction("Index");
}
I have a requirement of uploading the multiple documents with the help of file upload control. The same thing I had done for the Image type. It is working perfectly since it takes only 1 image file. But the multiple file upload is not working. What's the flaw in my code How can I do it?
Html Code:
----------
#using (Html.BeginForm("Index", "Block", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="col-lg-4" id="FileUp">
<label>Upload File</label>
#Html.TextBoxFor(m=>m.DocumentFilesPath, new { #name="fileUpload", #id="fileUpload", #type = "file", #multiple="multiple", #class="form-control dropDownLabel", #onchange="fileTypeCheck()" } )
</div>
// What should I specify the type for DocumentFilesPath in #Html.TextBoxFor(m=>m.DocumentFilesPath) in my strongly typed model?
<div class="col-lg-8>
<button type="button" class="btn btn-primary" id="SaveBlock" onclick="checkSubmit()">
<span class="glyphicon glyphicon-floppy-save"></span>Save
</button>
</div>
}
My Controller Action:
---------------------
[Authorize]
[HttpPost]
public ActionResult Index(BlockViewModel model)
{
if (model.BlockID == 0)
{
HttpPostedFileBase file = Request.Files["imgUpload"] as HttpPostedFileBase; // I am able to get the single file here
if (file.ContentLength > 0)
{
file.SaveAs(Server.MapPath("~/Images/uploads/") + file.FileName);
model.ImageFilepath = Server.MapPath("~/Images/uploads/") + file.FileName;
}
IEnumerable<HttpPostedFileBase> docFiles = Request.Files["fileUpload"] as IEnumerable<HttpPostedFileBase>; // But here docFiles is null
//IEnumerable<HttpPostedFileBase> docFiles = fileUpload as IEnumerable<HttpPostedFileBase>;
if (docFiles != null)
{
foreach (HttpPostedFileBase docFile in docFiles)
{
if (docFile.ContentLength > 0)
{
docFile.SaveAs(Server.MapPath("~/Images/Files/") + docFile.FileName);
model.DocumentFilesPath = Server.MapPath("~/Images/Files/") + docFile.FileName;
}
}
}
}
Refer this link for multiple file uploads ,,
http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx/
Basic Idea is We can simply have
multiple file inputs all with the same name.
<form action="" method="post" enctype="multipart/form-data">
<label for="file1">Filename:</label>
<input type="file" name="files" id="file1" />
<label for="file2">Filename:</label>
<input type="file" name="files" id="file2" />
<input type="submit" />
</form>
and in Controller
[HttpPost]
public ActionResult Index(IEnumerable<HttpPostedFileBase> files) {
foreach (var file in files) {
if (file.ContentLength > 0) {
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
}
return RedirectToAction("Index");
}
I want to fetch data from text file that is in particular folder into div tag..
MVC is new for me..So explain me briefly..
controller
public ActionResult filexist()
{
string subPath = "~/Content/74/74/6_Hall0/Text/data.txt";
bool exists = System.IO.Directory.Exists(Server.MapPath(subPath));
if (!exists)
System.IO.Directory.CreateDirectory(Server.MapPath(subPath));
else
{
}
return View();
}
HTML:
<form action="" method="post">
<div>
<textarea rows="3" cols="15" contenteditable="true" name="data"></textarea>
<input type="submit" value="Submit" />
</div>
</form>
first of all,
the default Method in the controller is "GET",
so if you want to use Post so you have to do like this:
in your Controller add "[HttpPost]":
[HttpPost]
public ActionResult filexist()
{
string subPath = "~/Content/74/74/6_Hall0/Text/data.txt";
bool exists = System.IO.Directory.Exists(Server.MapPath(subPath));
if (!exists)
System.IO.Directory.CreateDirectory(Server.MapPath(subPath));
else
{
}
return View();
}
and then, in your HTML(View):
#using (Html.BeginForm("filexist", "Your_Controller_Name", FormMethod.Post))
{
<div>
<textarea rows="3" cols="15" contenteditable="true" name="data"></textarea>
<input type="submit" value="Submit" />
</div>
}