Internet Explorer file download prompt character encoding - asp.net-mvc-4

In Internet Explorer running an MVC 4 application, when downloading a file with an Arabic filename, the download prompt displays a filename which appears to be in the wrong character set. Please see the attached image.
If it's possible to alter the display of this filename how do I do it?

If you are using the return File method, try encoding the file name
var fileName = Path.GetFileName(filePath);
if (Request.Browser.Browser == "IE")
{
string attachment = string.Format("attachment; filename=\"{0}\"",
Server.UrlPathEncode(fileName));
Response.AddHeader("Content-Disposition", attachment);
}
return File(filePath, "application/octet-stream", fileName);

Related

How to take accurate print from RDLC report without saving as pdf

I arrange the textbox in rdlc in correct width and height for pre printed page printing. As I take in pdf the alignment of all the labels come correct but I want to take the same without the file getting saved as pdf in system.
In short I don't want to take pdf but still want accurate print like pdf without the file getitng downloaded
enter image description here
I found a solution by rendering the report viewer into pdf and directly will be displayed in PDF Print
Warning[] warnings;
string[] streamIds;
string contentType;
string encoding;
string extension;
//Export the RDLC Report to Byte Array.
byte[] bytes = ReportViewer1.LocalReport.Render("PDF", null, out contentType, out encoding, out extension, out streamIds, out warnings);
// Open generated PDF.
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = contentType;
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
Refer
https://www.aspsnippets.com/questions/110559/Export-PDF-from-RDLC-Report-and-open-in-Browser-on-Button-Click-using-C-and-VBNet-in-ASPNet/

How to return a string as a CSV with proper encoding for Excel

I have the following code:
public async Task<ActionResult<FileResult>> GetCSV()
{
var stringCsv = await _statistics.GetUserCSV();
using (var stream = new MemoryStream())
using (var sw = new StreamWriter(stream, Encoding.UTF8))
{
sw.Write(stringCsv);
sw.Flush();
return File(stream.ToArray(), "text/csv", "thefile.csv");
}
}
If I inspect stringCsv while debugging it looks good, but If I look at the resulting CSV in Excel I get this.
The missing letters are the Swedish letters ÅÄÖ.
What am I doing wrong?
The issue is not coming from your code. It is from the file itself. Try the following:
On a Windows computer, open the CSV file using Notepad.
Click "File > Save As".
In the dialog window that appears - select "ANSI" from the "Encoding" field. Then click "Save".
That's all! Open this new CSV file using Excel - your non-English characters should be displayed properly.
Let me know if it worked.

ExtentReports in Selenium Webdriver

I'm having an issue where ExtentReports is not showing the screenshot when the report is viewed on another machine. When saving the image into the report, I pass the absolute path of the image file. The user who wants to view the report have to copy report.html and Errorscreenshot folder to their D drive. Then only they can see the screenshot. Please suggest another way so that user can copy these files to any of their location so that screenshot can be viewed.
My code is below:
TakesScreenshot ts = (TakesScreenshot)driver;
File source = ts.getScreenshotAs(OutputType.FILE);
String dest = "D:\\ErrorScreenshots\\"+screenShotName+System.currentTimeMillis()+".png";
File destination = new File("D:\\ErrorScreenshots\\"+screenShotName+System.currentTimeMillis()+".png");
FileUtils.copyFile(source, destination);
//FileUtils.copyFile(source, );
Instead of relative path. I found it is easier if the image is converted to base64 format. In that case, we only want to share .html file only.
TakesScreenshot ts = (TakesScreenshot)driver;
String dest = ts.getScreenshotAs(OutputType.BASE64);
return "data:image/jpg;base64, " + dest ;
I have used the extent report version 2.40.2 jar and have achieved sharing of my reports folder in this way. The paths are relative here and the report would render correctly with these paths. Hope this helps. I went through many links but nothing actually helped so came up with this logic.
public String capture() throws IOException {
try{
//take screenshot and save it in a file
File sourceFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
//copy the file to the required path
File destinationFile = new File(System.getProperty("user.dir")+"/reports/image_" + System.currentTimeMillis()+ ".png");
FileHandler.copy(sourceFile, destinationFile);
String[] relatvePath = destinationFile.toString().split("reports");
screenshotPath = ".\\" + relatvePath[1];
}
catch(Exception e){
//if it fails to take screenshot then this block will execute
System.out.println("Failure to take screenshot "+e);
}
return screenshotPath;
}
test.log(LogStatus.FAIL, Constants.REPORT_ERROR, test.addScreenCapture(capture()) + Constants.REPORT_ERROR_STATUS);
Use relative path for your screenshots. Save your screenshots where your html file is located.
Use relative path for screenshot as below:
String path = System.getProperty("user.dir")+"\\ErrorScreenshots\\"+screenShotName+System.currentTimeMillis()+".png";
File destination = new File(path);

Dialog to pick a file name like in the "Save As" dialog in Photoshop script

Is there an API to show a dialog that allows the user to browse/create folders and either pick an already existing file or enter the name for a new one in order to save some metadata.
Something like the "Save As" dialog in Photoshop but without actually saving anything, but just retrieve the path and filename entered by the user.
Not sure about a specific API, but is this the sort of thing you're looking for?
var inFolder = Folder.selectDialog("Please select folder to process");
if (inFolder != null)
{
var fileList = inFolder.getFiles(/\.(png)$/i);
}
or
var filterFiles = 'CSV:*.csv'; // I am windows
theFile = File.openDialog ("Choose the CSV file to load from" , filterFiles);
Whereas you can reference theFile or fileList to get the file or path you're after.

Load local HTML into WebView

Can I load a local HTML file (with images and ...) into a WebView?
Just setting the Source parameter does not do the trick.
You can load it from a file as long as the file is part of the app package, e.g.:
WebView2.Source = new Uri("ms-appx-web:///assets/text.html");
From WebView.Navigate
WebView can load content from the application’s package using
ms-appx-web://, from the network using http/https, or from a string
using NavigateToString. It cannot load content from the application’s
data storage. To access the intranet, the corresponding capability
must be turned on in the application manifest.
For a 'random' file, I suppose you could prompt user via file picker to select the file then read it into a string and use NavigateToString, but the user experience there may be a bit odd depending on what you're trying to accomplish.
I was working at this problem for a long time and I found a way to do that:
At first you should save it in InstalledLocation folder. If you haven't option to create a new .html file you can just use file.CopyAsync(htmlFolder, fname + ".html");
Look into my example:
StorageFolder htmlFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.CreateFolderAsync(#"HtmlFiles", CreationCollisionOption.GenerateUniqueName);
IStorageFile file = await htmlFolder .CreateFileAsync(fname + ".html", CreationCollisionOption.GenerateUniqueName);
and than you can easily open your .html file:
var fop = new FileOpenPicker();
fop.FileTypeFilter.Add(".html");
var file = await fop.PickSingleFileAsync();
if (file != null)
{
string myPath = file.Path.Substring(file.Path.IndexOf("HtmlFiles"));
myWebview.Navigate(new Uri("ms-appx-web:///" + myPath));
}
Remember just only from InstalledLocation you can open it with ms-appx-web:///