I have a page with a text area for title input and body input.
Saving a text file with those things is easy, the question is, how can I make the file to be named after whatever was placed in the title input?
I tried this:
#{
var result = "";
if (IsPost)
{
var title = Request["title"];
var body = Request["body"];
var filedata = title + "," + body + Environment.NewLine;
var dataFile = Server.MapPath("/App_Data/Request["title"]");
File.WriteAllText(#dataFile, filedata);
result = "Information saved.";
}
}
(Note that var title = Request["title"]; means that its requesting from a text input named "title"). What I want to get is that the input will also be the name of the file its saving.
But it seems that this area:
var dataFile = Server.MapPath("/App_Data/Request["title"]");
is not the correct way.
What is the correct way to do it?
Couple of pointers; firstly this sort of logic should be in a Controller, not in a View. Your Views are supposed to display information about your model, your Controllers carry out operations.
Secondly, the following should do the trick (in a Controller!):
[HttpPost]
public ActionResult SaveFile(string title, string body)
{
var fileData = title + "," + body + Environment.NewLine;
var fileSavePath = Path.Combine(
Server.MapPath("~/TextFiles"),
title.Replace(" ", "_") + ".txt");
File.WriteAllText(fileSavePath, fileData);
return this.RedirectToAction("SaveSuccessful");
}
Of note:
Server.MapPath("~/TextFiles") gives you the path to a TextFiles directory in the root of your web application where the files will be stored.
I've replaced spaces in the title which has been input with underscores.
This method redirects the user to an Action named SaveSuccessful on the same Controller
Of course you need error handling and all sorts of other things in there, but hopefully that helps.
Related
I encountered a problem with setting the page header text containing ampersand like ‘a&b’. The text after ‘&’ disappears in the pdf maybe because it is the reserved key in Aspose. My code looks like this:
PageSetup pageSetup = workbook.getWorksheets().get(worksheetName).getPageSetup();
//calling the function
setHeaderFooter(pageSetup, parameters, criteria)
//function for setting header and footer
def setHeaderFooter(PageSetup pageSetup, parameters, criteria = [:])
{
def selectedLoa=getSelectedLoa(parameters)
if(selectedLoa.length()>110){
String firstLine = selectedLoa.substring(0,110);
String secondLine = selectedLoa.substring(110);
if(secondLine.length()>120){
secondLine = secondLine.substring(0,122)+"...."
}
selectedLoa = firstLine+"\n"+secondLine.trim();
}
def periodInfo=getPeriodInfo(parameters, criteria)
def reportingInfo=periodInfo[0]
def comparisonInfo=periodInfo[1]
def benchmarkName=getBenchmark(parameters)
def isNonComparison = criteria.isNonComparison?
criteria.isNonComparison:false
def footerInfo="&BReporting Period:&B " + reportingInfo+"\n"
if (comparisonInfo && !isNonComparison){
footerInfo=footerInfo+"&BComparison Period:&B " +comparisonInfo+"\n"
}
if (benchmarkName){
footerInfo+="&BBenchmark:&B "+benchmarkName
}
//where I encounterd the issue,selectedLoa contains string with ampersand
pageSetup.setHeader(0, pageSetup.getHeader(0) + "\n&\"Lucida Sans,Regular\"&8&K02-074&BPopulation:&B "+selectedLoa)
//Insertion of footer
pageSetup.setFooter(0,"&\"Lucida Sans,Regular\"&8&K02-074"+footerInfo)
def downloadDate = new Date().format("MMMM dd, yyyy")
pageSetup.setFooter(2,"&\"Lucida Sans,Regular\"&8&K02-074" + downloadDate)
//Insertion of logo
try{
def bucketName = parameters.containsKey('printedRLBucketName')?parameters.get('printedRLBucketName'):null
def filePath = parameters.containsKey('printedReportLogo')?parameters.get('printedReportLogo'): null
// Declaring a byte array
byte[] binaryData
if(!filePath || filePath.contains("null") || filePath.endsWith("null")){
filePath = root+"/images/defaultExportLogo.png"
InputStream is = new FileInputStream(new File(filePath))
binaryData = is.getBytes()
}else {
AmazonS3Client s3client = amazonClientService.getAmazonS3Client()
S3Object object = s3client.getObject(bucketName, filePath)
// Getting the bytes out of input stream of S3 object
binaryData = object.getObjectContent().getBytes()
}
// Setting the logo/picture in the right section (2) of the page header
pageSetup.setHeaderPicture(2, binaryData);
// Setting the script for the logo/picture
pageSetup.setHeader(2, "&G");
// Scaling the picture to correct size
Picture pic = pageSetup.getPicture(true, 2);
pic.setLockAspectRatio(true)
pic.setRelativeToOriginalPictureSize(true)
pic.setHeight(35)
pic.setWidth(Math.abs(pic.getWidth() * (pic.getHeightScale() / 100)).intValue());
}catch (Exception e){
e.printStackTrace()
}
}
In this case, I get only ‘a’ in the pdf header all other text after ampersand gets disappeared. Please suggest me with a solution for this. I am using aspose 18.2
We have added header on a PDF page with below code snippet but we did not notice any problem when ampersand sign is included in header text.
// open document
Document document = new Document(dataDir + "input.pdf");
// create text stamp
TextStamp textStamp = new TextStamp("a&bcdefg");
// set properties of the stamp
textStamp.setTopMargin(10);
textStamp.setHorizontalAlignment(HorizontalAlignment.Center);
textStamp.setVerticalAlignment(VerticalAlignment.Top);
// set text properties
textStamp.getTextState().setFont(new FontRepository().findFont("Arial"));
textStamp.getTextState().setFontSize(14.0F);
textStamp.getTextState().setFontStyle(FontStyles.Bold);
textStamp.getTextState().setFontStyle(FontStyles.Italic);
textStamp.getTextState().setForegroundColor(Color.getGreen());
// iterate through all pages of PDF file
for (int Page_counter = 1; Page_counter <= document.getPages().size(); Page_counter++) {
// add stamp to all pages of PDF file
document.getPages().get_Item(Page_counter).addStamp(textStamp);
}
// save output document
document.save(dataDir + "TextStamp_18.8.pdf");
Please ensure using Aspose.PDF for Java 18.8 in your environment. For further information on adding page header, you may visit Add Text Stamp in the Header or Footer section.
In case you face any problem while adding header, then please share your code snippet and generated PDF document with us via Google Drive, Dropbox etc. so that we may investigate it to help you out.
PS: I work with Aspose as Developer Evangelist.
Well, yes, "&" is a reserved word when inserting headers/footers in MS Excel spreadsheet via Aspose.Cells APIs. To cope with your issue, you got to place another ampersand to paste the "& (ampersand)" in the header string. See the sample code for your reference:
e.g
Sample code:
Workbook wb = new Workbook();
Worksheet ws = wb.getWorksheets().get(0);
ws.getCells().get("A1").putValue("testin..");
String headerText="a&&bcdefg";
PageSetup pageSetup = ws.getPageSetup();
pageSetup.setHeader(0, headerText);
wb.save("f:\\files\\out1.xlsx");
wb.save("f:\\files\\out2.pdf");
Hope this helps a bit.
I am working as Support developer/ Evangelist at Aspose.
I am trying to use a string with double quotes but unable to get it work
#{
string disableMessage = "";
var disableAttr = "";
if (ViewBag.IsApplicable)
{
disableMessage = "You dont have permission to add new Item.";
disableAttr = "class=" + "disableItem" +" title="+"\""+ disableMessage +"\"";
}
}
expected: disableAttr as
class=disableItem title="You dont have permission to add new demand."
I got struck at getting double quotes for title attribute.
Why not deal with the two attributes separately:
#{
string disableTitle = null;
string disableClass = null;
if (ViewBag.IsApplicable)
{
disableTitle = "You dont have permission to add new Item.";
disableClass = "disableItem";
}
}
<div class="#disableClass" title="#disableTitle">Content</div>
Note that Razor V2 (in MVC4+) has a "conditional attribute" feature. When an attribute value is null, then Razor won't output anything at all for the attribute. So in the example above, if ViewBag.IsApplicable is false, the output will be:
<div>Content</div>
Ross's answer is much more elegant. However, keeping the line of your original code, you could do the following:
disableAttr = "class='disableItem'" +" title='"+ disableMessage +"'";
This will render the following text inside disableAttr:
class='disableItem' title='your_message_here';
I browsed around for a solution and I am sure it's a simple question but still not sure how to do that. So, I have a string that contains many words and some times it has links in it. For example:
I like the website http://somesitehere.com/somepage.html and I suggest you try it too.
I want to display the string in my view and have all links automatically converted to URLs.
#Model.MyText
Even StackOverflow gets it.
#Hunter is right.
In addition i found complete implementation in C#: http://weblogs.asp.net/farazshahkhan/archive/2008/08/09/regex-to-find-url-within-text-and-make-them-as-link.aspx.
In case original link goes down
VB.Net implementation
Protected Function MakeLink(ByVal txt As String) As String
Dim regx As New Regex("http://([\w+?\.\w+])+([a-zA-Z0-9\~\!\#\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?", RegexOptions.IgnoreCase)
Dim mactches As MatchCollection = regx.Matches(txt)
For Each match As Match In mactches
txt = txt.Replace(match.Value, "<a href='" & match.Value & "'>" & match.Value & "</a>")
Next
Return txt
End Function
C#.Net implementation
protected string MakeLink(string txt)
{
Regex regx = new Regex("http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\#\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);
MatchCollection mactches = regx.Matches(txt);
foreach (Match match in mactches) {
txt = txt.Replace(match.Value, "<a href='" + match.Value + "'>" + match.Value + "</a>");
}
return txt;
}
One way to do that would be to do a Regular Expression match on a chunk of text and replace that url string with an anchor tag.
Another regex that can be used with KvanTTT answer, and has the added benefit of accepting https urls
https?://([\w+?.\w+])+([a-zA-Z0-9\~!\##\$\%\^\&*()_-\=+\/\?.:\;\'\,]*)?
.net string representation:
"https?://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\#\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?"
What I have already : I have controller code churning up some values / calculations and sending them to the View. Right now, I am displaying all results on the view page.
What I am trying to do : Export those results that are displayed on the view to a text or excel file on press of a button "Export"
Using : MVC
Sample code I have
Controller:
public ActionResult Calculations()
{
dynamic CalcModel = new ExpandoObject();
int var1 = //Value calculated here
int var2 = //Calculation
CalcModel.Var1= var1;
CalcModel.Var2= var2;
return View(CalcModel);
}
View:
<table>
<tr><td>First Value:</td><td><%=Model.Var1%></td></tr>
<tr><td>Second Value:</td><td><%=Model.Var2%></td></tr>
</table>
I want to be able to write these values from the controller to a text file or excel file and let the user save the file. Thanks for the help.
EDIT:
I found a solution (kind of) but need further help:
Latest Controller code:
public ActionResult Calculations()
{
dynamic CalcModel = new ExpandoObject();
int var1 = //Value calculated here
int var2 = //Calculation
CalcModel.Var1= var1;
CalcModel.Var2= var2;
//Export code.
string csv = "Value1 = " + var1 + "|| Value2 = " + var2;
return File(new System.Text.UTF8Encoding().GetBytes(csv), "text/csv",
"Report.txt");
//Export code end.
return View(CalcModel);
}
This generates a text file with the values printed in it. But How do I get each value printed in a separate line.. right now all values are printed in one flat line.
Envrionment.NewLine should do it:
var builder = new StringBuilder();
//this is probably a loop...
builder.AppendFormat("1,2{0}", Envrionment.NewLine);
builder.AppendFormat("3,4{0}", Envrionment.NewLine);
File(new System.Text.UTF8Encoding().GetBytes(builder.ToString()), "text/csv",
"Report.txt");
Edit:
You can try \n as well.
string csvText = string.Format("First Value,{0}\nSecond Value,{1}",var1, var2);
File(new System.Text.UTF8Encoding().GetBytes(csvText), "text/csv",
"Report.txt");
This might help
http://stephenwalther.com/blog/archive/2008/06/16/asp-net-mvc-tip-2-create-a-custom-action-result-that-returns-microsoft-excel-documents.aspx
Or this:
http://www.codeproject.com/KB/aspnet/Streaming_Excel_ASP_NET.aspx
I want to see the cleaned HTML that we get from HTMLCleaner.
I see there is a method called serialize on TagNode, however don't know how to use it.
Does anybody have any sample code for it?
Thanks
Nayn
Here's the sample code:
HtmlCleaner htmlCleaner = new HtmlCleaner();
TagNode root = htmlCleaner.clean(url);
HtmlCleaner.getInnerHtml(root);
String html = "<" + root.getName() + ">" + htmlCleaner.getInnerHtml(root) + "</" + root.getName() + ">";
Use a subclass of org.htmlcleaner.XmlSerializer, for example:
// get the element you want to serialize
HtmlCleaner cleaner = new HtmlCleaner();
TagNode rootTagNode = cleaner.clean(url);
// set up properties for the serializer (optional, see online docs)
CleanerProperties cleanerProperties = cleaner.getProperties();
cleanerProperties.setOmitXmlDeclaration(true);
// use the getAsString method on an XmlSerializer class
XmlSerializer xmlSerializer = new PrettyXmlSerializer(cleanerProperties);
String html = xmlSerializer.getAsString(rootTagNode);
XmlSerializer xmlSerializer = new PrettyXmlSerializer(cleanerProperties);
String html = xmlSerializer.getAsString(rootTagNode);
the method above has a problem,it will trim content in html label, for example,
this is paragraph1.
will become
this is paragraph1.
and it is getSingleLineOfChildren function does the trim operation. So if we fetch data from website and want to keep the format like tuckunder.
PS:if a html label has children label,the parent label contetn will not be trimed,
for example <p> this is paragraph1. <a>www.xxxxx.com</a> </p> will keep whitespace before "this is paragraph1"