Here is the situation.
I have an action method, which returns a file, called "GetDocument" in "Documents" controller. It has one parameter of type Document, which contains document path, title, type etc.
I have a View for an entity, which has some documents attached with it. For example, a news story, with attached documents. On this view I show various links for the documents which the end user should be able to download.
The question is: How do I create such links which pass proper "Document" object to the "GetDocument" Action Method?
Edit: I do not want to show full path of the document to the user. In fact, I would like that I store documents in App_Data folder, so that they cannot be downloaded otherwise.
Thanks!
You could use an ActionLink:
#Html.ActionLink(
"Download",
"GetDocument",
new {
path = "report.pdf",
type = "application/pdf"
}
)
and then:
public ActionResult GetDocument(Document doc)
{
var appData = Server.MapPath("~/App_Data");
var file = Path.Combine(appData, doc.Path);
file = Path.GetFullPath(file);
if (!file.StartsWith(appData))
{
// Ensure there are no cheaters trying to read files
// outside of the App_Data folder like "../web.config"
throw new HttpException(403, "Forbidden");
}
if (!File.Exists(file))
{
return HttpNotFound();
}
return File(file, doc.Type, Path.GetFileName(file));
}
why would your controller need to know about the full document when the action is called? surely your documents have ids. take the id, ask your repository for the domain object, and then get the file.
Related
Is there a way to get the full file path using a file browser and send that path as a String using Razor on MVC?I've been using a file selector but this only uploads the file name. Here's the code I have on my cshtml file:
#Html.TextBoxFor(m => m.IMG, new { type = "file" })
#Html.ValidationMessageFor(m => m.IMG)
But this methods only sends the name of the file as a string, not the full path.
For example instead of getting the path "C:\Users\UserMame\Pictures\Test.jpg", it only returns "Test.jpg"
You can do one thing. while saving the image,before saving add full path like
string rootpath = "~/Image/MyImages/";//change it according to your requirement
if (img != null)
{
if (!(Directory.Exists(Server.MapPath(rootpath))))
{
Directory.CreateDirectory(Server.MapPath(rootpath));//create a new directory if directory is doesn't exist in the root folder
}
string path = System.IO.Path.Combine(Server.MapPath(rootpath), "Test.jpg");
img.SaveAs(path);
}
you can use MapPath in the controller :
model.IMG=Server.MapPath("~/App_Data/"+Model.IMg);
and then pass the model to view
I want to delete a pdf file form my database as well as my public/uploads folder. It is deleting from the database but not from my public folder.
This is my controller:
public function deleteArticle($id) {
$article = Article::findOrFail($id);
File::delete($article->document);
$article->delete();
return redirect()->back();
}
/*This handles the posting of the file into the folder and storing of the url into the datab
$file = Input::file('document');
$file->move('uploads', $file->getClientOriginalName());
$document = asset('uploads/'.$file->getClientOriginalName());
$newArticle->document = $document;
As you are currently saving a url to the database ( by using the asset() function ) you can't delete the file by using that information.
It is usually enough to save just the document name in the database.
$document = $file->getClientOriginalName();
$newArticle->document = $document;
To delete the file you can then call:
File::delete(public_path('uploads/'.$article->document);
To Link to your file you can use the asset() method
asset('uploads/'.$article->document);
Storing the full URL in database is a bad idea. It will very hard to maintain files later. The best way is store only the filename with extension.
If you only have the filename in database, you can delete the file in this way:
$article = Article:findOrFail($id);
$document = $article->document; // take the image name from database
File::delete('uploads/'.$document); // delete the file
$article->delete() // delete the record from database
Edit
If you still want to use URL in database you can get the image name by using substr() and strpos() function. Example:
$image = substr($article->document,0,strpos("uploads/"));
You can get only the document name from URL and use it to delete the file.
To store only the filename follow this:
$document = $request->file('document')->getClientOriginalName();
I am trying to make an asp.net mvc 5 app by this ASP.NET image upload guide.
I am curious about this part of the guide (on the photo here):
as I understand at that part I am supposed to write the code in my View (cshtml) page. (Cause before there was attaching js's and running scripts in "<"script">" tag)
How can I write such C# code in the View? Where am I wrong?
string cors_location = (new UriBuilder(Request.Url.AbsoluteUri) { Path = Url.Content("~/Content/cloudinary_cors.html") }).ToString();
C# code in a view generally belongs in a server side block like this ...
#{
string cors_location = (new UriBuilder(Request.Url.AbsoluteUri)
{ Path = Url.Content("~/Content/cloudinary_cors.html") }).ToString();
}
How can I write such C# code in the View? Where am I wrong?
Hi.
like this by adding the '#'sign
#{string cors_location = (new UriBuilder(Request.Url.AbsoluteUri) { Path = Url.Content("~/Content/cloudinary_cors.html") }).ToString();}
also please check http://www.asp.net/web-pages/overview/getting-started/introducing-razor-syntax-c
my server should get a link of uploaded file
To manage this You should do something like this:
create property in Your model class for eg.
public string ImgUrl {get;set;}
In Your View controller you should pass url value from view
Img.Url = "My Image Url passed from View";
and then save Your model to Db.
For more please check http://www.mikesdotnetting.com/article/259/asp-net-mvc-5-with-ef-6-working-with-files
I have an MVC Razor application where I am returning a view.
I have overloaded my action to accept a null-able "export" bool which will change the action by adding headers but still returning the same view as a file (in a new window).
//if there is a value passed in, set the bool to true
if (export.HasValue)
{
ViewBag.Exporting = true;
var UniqueFileName = string.Format(#"{0}.xls", Guid.NewGuid());
Response.AddHeader("content-disposition", "attachment; filename="+UniqueFileName);
Response.ContentType = "application/ms-excel";
}
As the file was generated based on a view, its not an .xls file so when opening it, I get the message "the file format and extension of don't match". So after a Google, I have found THIS POST on SO where one of the answers uses VBA to open the file on the server (which includes the HTML mark-up) then saves it again (as .xls).
I am hoping to do the same, call the controller action which will call the view and create the .xls file on the server, then have the server open it, save it then return it as a download.
What I don't want to do is to create a new view to return a clean file as the current view is an extremely complex page with a lot of logic which would only need to be repeated (and maintained).
What I have done in the view is to wrap everything except the table in an if statement so that only the table is exported and not the rest of the page layout.
Is this possible?
You can implement the VBA in .net
private void ConvertToExcel(string srcPath, string outputPath, XlFileFormat format)
{
if (srcPath== null) { throw new ArgumentNullException("srcPath"); }
if (outputPath== null) { throw new ArgumentNullException("outputPath"); }
var excelApp = new Application();
try
{
var wb = excelApp.Workbooks.Open(srcPath);
try
{
wb.SaveAs(outputPath, format);
}
finally
{
Marshal.ReleaseComObject(wb);
}
}
finally
{
excelApp.Quit();
}
}
You must install Microsoft.Office.Interop and add reference to a COM oject named Microsoft Excel XX.0 Object Library
Sample usage:
//generate excel file from the HTML output of GenerateHtml action.
var generateHtmlUri = new Uri(this.Request.Url, Url.Action("GenerateHtml"));
ConvertToExcel(generateHtmlUri.AbsoluteUri, #"D:\output.xlsx", XlFileFormat.xlOpenXMLStrictWorkbook);
I however discourage this solution because:
You have to install MS Excel in your web server.
MS Excel may sometimes misbehave like prompting a dialog box.
You must find a way to delete the generated Excel file afterwards.
Ugly design.
I suggest to generate excel directly because there doesn't seem to be better ways to covert HTML to Excel except using Excel itself or DocRaptor.
I have kept a word document (.docx) in one of the project folders which I want to use as a template.
This template contains custom header and footer lines for user. I want to facilitate user to download his own data in word format. For this, I want to write a function which will accept user data and referring the template it will create a new word file replacing the place-holders in the template and then return the new file for download (without saving it to server). That means the template needs to be intact as template.
Following is what I am trying. I was able to replace the placeholder. However, I am not aware of how to give the created content as downloadable file to user. I do not want to save the new content again in the server as another word file.
public void GenerateWord(string userData)
{
string templateDoc = HttpContext.Current.Server.MapPath("~/App_Data/Template.docx");
// Open the new Package
Package pkg = Package.Open(templateDoc, FileMode.Open, FileAccess.ReadWrite);
// Specify the URI of the part to be read
Uri uri = new Uri("/word/document.xml", UriKind.Relative);
PackagePart part = pkg.GetPart(uri);
XmlDocument xmlMainXMLDoc = new XmlDocument();
xmlMainXMLDoc.Load(part.GetStream(FileMode.Open, FileAccess.Read));
xmlMainXMLDoc.InnerXml = ReplacePlaceHoldersInTemplate(userData, xmlMainXMLDoc.InnerXml);
// Open the stream to write document
StreamWriter partWrt = new StreamWriter(part.GetStream(FileMode.Open, FileAccess.Write));
xmlMainXMLDoc.Save(partWrt);
partWrt.Flush();
partWrt.Close();
pkg.Close();
}
private string ReplacePlaceHoldersInTemplate(string toReplace, string templateBody)
{
templateBody = templateBody.Replace("#myPlaceHolder#", toReplace);
return templateBody;
}
I believe that the below line is saving the contents in the template file itself, which I don't want.
xmlMainXMLDoc.Save(partWrt);
How should I modify this code which can return the new content as downloadable word file to user?
I found the solution Here!
This code allows me to read the template file and modify it as I want and then to send response as downloadable attachment.