After I uploaded file using file uplaod control, and assigning it's name in controller, i need to get that file name in view. Also with that file name how to delete that file in my local drive(which is previously uploaded).
thanks.
check it please
public ActionResult SaveFile(HttpPostedFileBase FileUpload)
{
string path = string.Empty;
if (FileUpload != null)
if (FileUpload.ContentLength > 0)
{
fileName = Path.GetFileName(FileUpload.FileName);
path = Path.Combine(Server.MapPath("~/App_Data/Documents"), fileName);
FileUpload.SaveAs(path);
}
return View();
}
and don't forget to add enctype = "multipart/form-data" attribute to form
to can use this jQuery plugin to upload file via ajax jQuery File Upload in ASP.NET MVC
Related
I tried to add a custom column to Telerik Grid for uploading documents, but I am receiving 0 files in controller.
I think it's because name of form input field is different than parameter I receive in controller, Telerik adds column name (Document1) to left of upload filed name, see screenshot.
And this is my controller, see the parameter is file but in source of HTML the input field name is Document1.file
[HttpPost]
public async Task<ActionResult> SaveAsync(IEnumerable<IFormFile> file)
{
// The Name of the Upload component is "files"
if (file != null)
{
foreach (var f in file)
{
var fileContent = ContentDispositionHeaderValue.Parse(f.ContentDisposition);
// Some browsers send file names with full path.
// We are only interested in the file name.
var fileName = Path.GetFileName(fileContent.FileName.ToString().Trim('"'));
var physicalPath = Path.Combine(new HostingEnvironment().WebRootPath, "App_Data", fileName);
// The files are not actually saved in this demo
using (var fileStream = new FileStream(physicalPath, FileMode.Create))
{
await f.CopyToAsync(fileStream);
}
}
}
// Return an empty string to signify success
return Content("");
}
But I can't add a dot to parameter in controller!
How can I force it to for example name parameter as Document1_file?
I have MVC application throwing error 'Access to path XXX denied' while uploading video. Error is not thrown when image is being uploaded.
Any thing wring in my code?
[HttpPost]
public ActionResult Index(HttpPostedFileBase video)
{
//var httpPostedFile = Request.Files[0];
var ffMpeg = new NReco.VideoConverter.FFMpegConverter();
//ffMpeg.GetVideoThumbnail(Server.MapPath("~/Images"), "video_thumbnail.jpg");
var fileName = Path.GetFileName(video.FileName);
var path = Server.MapPath("~/Images");
video.SaveAs(path);
ffMpeg.GetVideoThumbnail(path, "video_thumbnail.jpg");
return View();
}
Well, I would say you have a problem here
video.SaveAs(path);
as path doesn't contains file name (you try to save with the directory as "fileName").
So
var path = Server.MapPath("~/Images");
var fileName = Path.Combine(path, video.FileName);
video.SaveAs(fileName);
I have the following issue: ASP-MVC
I want to put a file in a folder in IIS and allow users surfing my site to download it.
In my site, I will have a link that points to an action method in my controller, and within this method I want to put the needed code. Never dealt with this issue before, will appriciate a code sample. Thanks!
This code , taken from this question, will accomplish what you want.
public FileResult Download()
{
byte[] fileBytes = System.IO.File.ReadAllBytes("c:\folder\myfile.ext");
string fileName = "myfile.ext";
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}
Assuming you want to get a specific file based on some passed-in ID, you can use the Controller.File function as described here: http://msdn.microsoft.com/en-us/library/dd492492(v=vs.100).aspx
Here's an example controller function from that page:
public ActionResult ShowFileFN(string id) {
string mp = Server.MapPath("~/Content/" + id);
return File(mp, "text/html");
}
This will return a binary stream of the named file with the specified MIME content type, in this case "text/html". You'll need to know the MIME type for each file you're returning.
Here's a function to get the MIME type of a file based on its extension:
public static string GetMimeType(string fileName)
{
string mimeType = "application/unknown";
string ext = System.IO.Path.GetExtension(fileName).ToLower();
Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (regKey != null && regKey.GetValue("Content Type") != null)
mimeType = regKey.GetValue("Content Type").ToString();
return mimeType;
}
I use DropzoneJS with MVC. The file uploads fine, but action will not display another view, neither will display another view after redirected to another action. Just stays on the same view it was called from.
Action :
[HttpPost]
public ActionResult FileUpload(HttpPostedFileBase file)
{
if(file != null)
{
string ext = Path.GetExtension(file.FileName);
if (file.ContentLength > 0 && ext == ".txt")
{
var fileName = Path.GetFileName(file.FileName);
if (fileName != null)
{
var path = Path.Combine(Server.MapPath("~/uploads"), fileName);
file.SaveAs(path);
}
}
}
return View("Report");
// This will redirect to action but will not display another view either:
// return RedirectToAction("Report");
}
View called from:
<div id="dropzone">
<form action="/Dashboard/FileUpload" class="dropzone clickable" id="demo-upload" method="post" enctype="multipart/form-data">
</form>
</div>
you need to tell your browser to do the redirect, if you use dropzone to upload files asyncronsly. With a async call to your MVC controller, MVC can't tell the browser to change page. You can do a redirect in javascript after dropzone have uploaded the files with the complete event:
myDropzone.on("complete", function(file) {
window.location = "./Dashboard/Report/";
});
Background
I have a secured folder containing secret report files (in pdf format). Each file corresponds to a single user.
My site is developed using Asp.net MVC 3 Razor.
When a user navigate to http://mydomain.com/UserProfile/Report, it invokes the GET-handling action method Report and return a view with a submit button Download Report.
When the user click the button, it invokes the POST-handling action method. Verification will be done in this action method. When the verification is successfully passed, the action method returns the requested file.
Question
Could you give me an example how to implement the POST-handling action method?
Here is the skeleton:
[HttpPost]
public ActionResult Report(/*any parameter I don't know*/)
{
if(!IsAuthenticated())
return RedirectToActionLink("SomeActionMethod","SomeController");
else
{
// what should I do here?
// Asssume my secret folder is d:\mydomain.com\secret and the public folder is d:\mydomain.com\httpdoc
}
}
Here is how you can return a file back to the client:
public FileContentResult Report(/*any parameter I don't know*/)
{
if(!IsAuthenticated())
return RedirectToActionLink("SomeActionMethod","SomeController");
else
{
// Read the file from your location into a byte array named content
Response.ContentType = "application/pdf";
return new FileContentResult(content, "application/pdf");
}
}
You can use the File method to return a FileContentResult
[HttpPost]
public FileContentResult Report()
{
if(!IsAuthenticated())
return RedirectToActionLink("SomeActionMethod","SomeController");
else
{
string path = #"d:\mydomain.com\secret\" + fileName;
return File(path, "application/pdf"); ////
}
}