How to download a file to client from server? - asp.net-mvc

I have an MVC project where I'd like the user to be able to download a an excel file with a click of a button. I have the path for the file, and I can't seem to find my answer through google.
I'd like to be able to do this with a simple button I have on my cshtml page:
<button>Button 1</button>
How can I do this? Any help is greatly appreciated!

If the file is not located inside your application folders and not accessible directly from the client you could have a controller action that will stream the file contents to the client. This could be achieved by returning a FileResult from your controller action using the File method:
public ActionResult Download()
{
string file = #"c:\someFolder\foo.xlsx";
string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
return File(file, contentType, Path.GetFileName(file));
}
and then replace your button with an anchor pointing to this controller action:
#Html.ActionLink("Button 1", "Download", "SomeController")
Alternatively to using an anchor you could also use an html form:
#using (Html.BeginForm("Download", "SomeController", FormMethod.Post))
{
<button type="submit">Button 1</button>
}
If the file is located inside some non-accessible from the client folder of your application such as App_Data you could use the MapPath method to construct the full physical path to this file using a relative path:
string file = HostingEnvironment.MapPath("~/App_Data/foo.xlsx");

HTML:
<div>#Html.ActionLink("UI Text", "function_name", "Contoller_name", new { parameterName = parameter_value },null) </div>
Controller:
public FileResult download(string filename) {
string path = "";
var content_type = "";
path = Path.Combine("D:\file1", filename);
if (filename.Contains(".pdf"))
{
content_type = "application/pdf";
}
return File(path, content_type, filename);
}

Related

How to open Pdf in Browser and Save as Pdf file in Folder in Asp.net mvc

I want to use the opening Pdf file, and the User inputs some of the fields and then saves this file into the Specific folder in my Asp.net folder.
I try to use the HttpPostedFileBase file but I cannot save this file to a folder, always become null for Parameter. Is it working right? You guys have any other idea for open Pdf and edit, save as pdf file to a folder?
<form method="post" enctype="multipart/form-data">
<div>
<embed name="file" src="~/AnnounceFile/PDF.pdf"
style="width: 780px; height:980px;"
class="with-200" />
<button type="submit">Import</button>
</div>
</form>
Controller
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
string filename = Guid.NewGuid() + Path.GetExtension(file.FileName);
string filepath = "/folder/" + filename;
file.SaveAs(Path.Combine(Server.MapPath("/excelfolder"), filename));
InsertExceldata(filepath, filename);
return View(db.Iteminfoes.ToList());
}
You can open the pdf using target="_blank" in a href tag
File Name
The PDFHelper.GeneratePDF is returning array of bytes of PDF file. As I understood, after that you need to store this PDF in local folder. In that case you can use.
protected void save_pdf()
{
String path_name = "~/PDF/";
var pdfPath = Path.Combine(Server.MapPath(path_name));
var formFieldMap = PDFHelper.GetFormFieldNames(pdfPath);
string username = "Test";
string password = "12345";
String file_name_pdf = "Test.pdf";
var pdfContents = PDFHelper.GeneratePDF(pdfPath, formFieldMap);
File.WriteAllBytes(Path.Combine(pdfPath, file_name_pdf), pdfContents);
WebRequest request = WebRequest.Create(Server.MapPath("~/PDF/" + pdfContents));
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
Stream reqStream = request.GetRequestStream();
reqStream.Close();
}
Also, please see Can a Byte[] Array be written to a file in C#?

Download .txt file from another server into local (ASP MVC)

I have successfully download file from my local into my local in ASP MVC.
View
#using (Html.BeginForm("Download", "Home", FormMethod.Post))
{
<button class="btn btn-primary" >Download</button>
}
controller
public ActionResult Download()
{
string file = #"C:\Users\Xin\Desktop\test.txt";
string contentType = "text/plain";
return File(file, contentType, Path.GetFileName(file));
}
What I want to ask is, how to do it if the file is not in my local, but it is on different server? let say server name called VUP-1 and the path on the server is C:\Users\Xin\Documents\test.txt
You are going to download files from Server where you have kept files.
You can used Server.MapPath and pass file path.
public ActionResult Download()
{
string file = Server.MapPath("~/Files/Demo.txt");
string contentType = "text/plain";
return File(file, contentType, Path.GetFileName(file));
}

Opening PDF in browser at a specific page

I am using a custom html helper #Html.ActionLink to find and return a PDF file to the browser and open in a new tab. The issue I am now having is when I am trying to open the PDF on a specific page.
The file is found, returned and opened just fine when NOT trying to specify a page parameter for the PDF to open on. Therefore, there must be some issue with my approach to setting the parameter.
According to Adobe documentation, #page=? parameters can be appended to the end of a URL to open a PDF at a specific page. However, my approach for doing so is not working.
See Adobe documentation on the subject here.
Razor helper in use:
#Html.FileLink("Document Link", "\\MyLocation\\MyDocument.pdf", "4", new { #target = "_blank" })
Helper method:
public static MvcHtmlString FileLink(this HtmlHelper helper, string LinkText, string FilePath, string PageNumber, object htmlAttributes = null)
{
return helper.ActionLink(LinkText, "ShowFile", "Home", new { path = System.Uri.EscapeDataString(FilePath), page = PageNumber }, htmlAttributes);
}
ShowFile method:
public ActionResult ShowFile(string path, string page)
{
// My attempt at passing and setting the page parameter!
path = System.Uri.UnescapeDataString(path + "#page=" + page);
// Get actual path to file, file name
var filePath = string.Format("{0}{1}", ConfigurationManager.AppSettings["DocumentsRoot"], path);
// Get MIME type
var contentType = MimeMapping.GetMimeMapping(path);
// Return file
return File(filePath, contentType);
}
The page parameter in the adobe documentation is an anchor tag (e.g. #page=5), not a query string parameter (?page=5). You can use a different ActionLink override to specify this at the same time:
Html.ActionLink(LinkText, "ShowFile", "Home", null, null, "page=" + PageNumber,
new { path = System.Uri.EscapeDataString(FilePath) }, null)
This will generate a link which looks like...
/Home/ShowFile?path=myfilename.txt#page=5
instead of
/Home/ShowFile?path=myfilename.txt&page=5
You can then remove the page parameter from your ShowFile method, because it is only needed on the client side.

File Upload in MVC 5

I am developing a MVC 5 application and using MS SQL Server as a database. I have form in this app, which will store the event details in database. in this form i have a file upload field. Actually i want to upload an image in a folder on the server and store its URL in the database so that URL could be used in my front end.
Following is my create action method
public ActionResult Create([Bind(Include = "Id,Event_Name,Event_Description,Event_Detail,Image_Url,Event_Date,User_Name,Date_Uploaded,Category_ID")] WASA_Events wASA_Events)
{
var filePath = FileUpload();//Function call to get the uploaded file path
wASA_Events.Image_Url = filePath;
if (ModelState.IsValid)
{
db.WASA_Events.Add(wASA_Events);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.Category_ID = new SelectList(db.WASA_Events_Category, "id", "Event_Category", wASA_Events.Category_ID);
return View(wASA_Events);
}
and FileUpload Function which will return the file path is as under
public string FileUpload()
{
var filePath = "";
if(Request.Files.Count > 0)
{
var file = Request.Files[0];
if(file!=null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/images/uplods/"), fileName);
file.SaveAs(path);
filePath = path;
}
}
return (filePath);
}
and in my view i used the following
#using (Html.BeginForm("Create", "WASA_Events", FormMethod.Post, new { enctype = "multipart/form-data" }))
Now the problem is i got nothing in the Request.Files.Count, means its value is zero. So can't move ahead.
Any Help.
Check the below.,.,
Check whether you can get the files in the request in the Create Action itself. I am sure that the request will maintain state in the FileUpload method too. But check for if it's not.
Whether the file upload input is inside the form that you are using in the view ?
Whether the file upload input is any third party control ? If so, check if you have the file name is updated in the file upload input in the HTML after selecting the file in the browser.
A little crazy check would be whether you have selected a file and opted to upload the file in the browser.,.,
I just did the file upload by adding the following class
public class Pictures
{
public HttpPostedFileBase File { get; set; }
}
and then use it in my create Controller action method. Below is the code for controller action
if (picture.File.ContentLength > 0)
{
var fileName = Path.GetFileName(picture.File.FileName);
var path = Path.Combine(Server.MapPath("~/assets/uploads/events/"), fileName);
picture.File.SaveAs(path);
}
and in view
<input type="file" id="File" name="File" class="form-control"/>
this solvedmy problem

Calls for files are made in the wrong file path

Hi I am trying to build an Angular App and I have the following situation in ASP.NET MVC.
I have created an App folder in the root directory of the project that contains the following:
styles folder
scripts folder
images folder
index.html file
Then instead of returning a View in my HomeController Index Action I returned the following:
public ActionResult Index()
{
return File(Server.MapPath("/App/index.html"), "text/html");
}
the index.html has a the following style added:
<link rel="stylesheet" href="styles/main.css">
When I run the app the index.html gets retrieved but I get an error inside of retrieving the style file:
http://localhost:57826/styles/main.css
For some reason instead of it looking in the the following path http://localhost:57826/App/styles/main.css it looks in the path mentioned above.
I then tryed to intercept the calls and get the files from the correct path by creating a custom router like this:
routes.MapRoute(
name: "Style",
url: "styles/{*path}",
defaults: new { controller = "Www", action = "Style" }
);
public class WwwController : Controller
{
public ActionResult Style(string path)
{
byte[] content = this.GetContent(this.CorrectPath(path, "css"));
var result = new FileContentResult(content, this.GetContentTypeFor(path));
return result;
}
private string CorrectPath(string path, string folder)
{
return Server.MapPath("/App/" + folder + "/" + path);
}
private byte[] GetContent(string path)
{
byte[] readAllBytes = System.IO.File.ReadAllBytes(path);
return readAllBytes;
}
private string GetContentTypeFor(string path)
{
return System.Web.MimeMapping.GetMimeMapping(Path.GetFileName(path));
}
}
But my solution does not seem to work.The Style action is not called.Can anyone tell me how can I solve my problem
Why are you breaking the MVC model by not allowing your view to do the presentation work? Simply do this:
public ActionResult Index() {
return View();
}
Then put whatever content you need into Views/Home/Index.cshtml:
<link rel="stylesheet" href="styles/main.css">
#* Add link tags, javascript, etc. *#
Or just use a vanilla ASP.NET site.

Resources