Atomically download a CVS file in .NET core - file

I am making a dynamic CVS report file in my controller in the format of StringBuilder. I have tried this code to return the file:
public async Task GetCvsAsync(....)
{
....
//making the file
using (MemoryStream stream = new MemoryStream())
{
StreamWriter objstreamwriter = new StreamWriter(stream);
objstreamwriter.Write(res.ToString());
objstreamwriter.Flush();
objstreamwriter.Close();
return File(stream.ToArray(), "application/octet-stream", "test.csv");
}
}
it makes the correct result but in the console response header (nothing happens in the front) like this:
if I right-click on the console and click open in a new tab, it will start to download the file. I have tried this way as well:
var f = File(Encoding.UTF8.GetBytes(res.ToString()), "text/csv", "authors.csv");
return f;
res is in the type of StringBuilder.
I want to download automatically begins after the file is ready, I have no idea what should I do.
note:
this action called by a href tag onClick event on my razor page:
function cvs(meID, oID, tID) {
$.get("/Dashboard/GetCVS?meID=" + meID
+ "&oID=" + oID+ "&tID=" + tID);
}

I couldn't solve my problem with jquery I just change my code to direct calling my action instead of ajax call and my problem solved!

Related

Why is IE ignoring my attachment filename content-disposition?

I'm using ASP.NET Web API targeting the 4.5.2 framework and am trying to push out a CSV file generated by exporting data from a table. In Firefox and Chrome, everything works as expected, but with IE (I'm testing with 11), the filename is being ignored and IE is using the URL instead (with no extension). What am I doing wrong and how can I get around it?
Here's my controller method:
[HttpGet]
public HttpResponseMessage ExportToCSV([FromUri]DistributionSearchCriteria criteria)
{
// This creates the csv in the temp folder and returns the temp file name
var file = _repository.ExportToCSV(criteria);
// This FileHttpResponseMessage is a custom type which just deletes the temp file in dispose
var result = new FileHttpResponseMessage(file, HttpStatusCode.OK)
{
Content = new StreamContent(File.OpenRead(file))
};
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
{
FileName = "Distributions.csv"
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return result;
}
Here's the custom FileHttpResponseMessage
public class FileHttpResponseMessage : HttpResponseMessage
{
private string _filePath;
public FileHttpResponseMessage(string filePath, HttpStatusCode statusCode) : base(statusCode)
{
_filePath = filePath;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
Content.Dispose();
if (File.Exists(_filePath))
{
try
{
System.Diagnostics.Debug.WriteLine("Deleting {0}", (object)_filePath);
File.Delete(_filePath);
System.Diagnostics.Debug.WriteLine("{0} deleted", (object)_filePath);
}
catch
{
System.Diagnostics.Debug.WriteLine("Error Deleting {0}", (object)_filePath);
}
}
}
}
}
and these are my two JavaScript methods in my AngularJS controller which launch the downloads:
vm.exportToCSV = function () {
var params = $httpParamSerializer(vm.searchCriteria);
$window.open(apiBase + 'Distribution/ExportToCSV?' + params, '_blank');
};
vm.exportAllToCSV = function () {
$window.open(apiBase + 'Distribution/ExportToCSV', '_blank');
};
From what I've read in other questions... setting the attachment; filename= should've been sufficient for IE. IE is prompting for a filename of "ExportToCSV".
I've also tried appending a bogus parameter like ?/distribution.csv and it changed the download filename but instead of distribution.csv it replaced the . with _ so the result was distribution_csv. Oh the pains of IE.
Update 1:
I've created a separate project to address only this issue and come up with specific workarounds. I've tried with and without quotes around the filename but I'm still no difference:
Update 2:
So I thought I would attempt to be "clever" and try to create a custom HTTP Handler for files with an extension:
Web.config
<!-- Route all files through asp -->
<modules runAllManagedModulesForAllRequests="true" />
<!-- Route all files through asp -->
<add name="FileDownloadHandler" path="/api/File/test.csv" verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>
WebApiConfig.cs
config.Routes.MapHttpRoute(
name:"download",
routeTemplate: "api/File/test.csv",
defaults: new { controller = "File", action = "Get" }
);
as expected it used the test.csv but it replaced the . with _ resulting in a test_csv extensionless download.
This is the weirdest bug I've ever ran into. I'm not sure whether to attribute it to my anitvirus or not, but my CPU recently maxed out at 100% as a result of McAFEE going bananas and bringing my system to a crawl. As a result, I rebooted my machine. Now Internet Explorer is working as expected! I never would've guessed... I was beginning to question everything I thought I knew...
Moral of the story:

How to test Android toast messages in Appium ( selenium Java)

I am using Selenium with Java to run scripts on android (thru Appium server).
I see that it is not possible to locate a toast by using selenium's
driver.findElement(By.LinkText("User not logged in")
in Appium
But can be used in Selendroid to capture toast messages.
I there a way I can use both Selendroid and Appium in the same script?
Finally, we are able to read the toast message without the need of taking screenshots and performing OCR.
I have tested this on Appium 1.15.1.
Toast messages comes under com.package.system.
Normally, Xpath for this will be "/hierarchy/android.widget.Toast".
And, Class Name will be "android.widget.settings"
You can confirm this by refreshing element inspector screen when toast message is displayed.
WebDriverWait waitForToast = new WebDriverWait(driver.25);
waitForToast.until(ExpectedConditions.presenceOfElementLoacted(By.xpath("/hierarchy/android.widget.Toast")));
String toastMessage = driver.findElement((By.xpath("/hierarchy/android.widget.Toast")).getText();
System.out.println(toastMessage);
Method 1: from Appium version 1.6.4 supports toast messages, for that you need to use automationName:'uiautomator2'.
toast = driver.find_element(:xpath, "//android.widget.Toast[1]")
if toast.text == "Hello"
But i don't recommend this because uiautomator2 is not stable yet.
Method 2:
Trigger text message on the screen
Capture screenshots
Convert image to text file
def assettoast(string)
sname = (0...8).map { (65 + rand(26)).chr }.join
$driver.driver.save_screenshot("#{sname}")
# Make sure tesseract is installed in the system. If not you can install using "brew install tesseract" in mac
system ("tesseract #{sname} #{sname}")
text_file="#{sname}.txt"
var= get_string_from_file(string, text_file)
raise if var != true
end
Check whether toast message is there in text file
def get_string_from_file(word, filename)
File.readlines(filename).each do |line|
return true if line.include?(word)
end
end
Looks like you can't switch driver type within same session.
If you trying to switch to Selendroid only for toast verification - you could use OSR image recognition engine.
Check this answer w/ Ruby bindings
Idea is quite simple:
make toast message to appear
take few screenshots
iterate over taken screenshots and look for required text
Here is nice and simple example of OCR usage in Java: tess4j example (make sure that Tesseract engine installed)
Step 1:
File scrFile=null;
String path1 = null;
BufferedImage originalImage=null;
BufferedImage resizedImage=null;
System.out.println("Starting\n\n\n\n");
scrFile = ((TakesScreenshot) appiumDriver).getScreenshotAs(OutputType.FILE);
System.out.println("after scrfile\n\n\n\n");
originalImage = ImageIO.read(scrFile);
System.out.println("after originalFile\n\n\n");
BufferedImage.TYPE_INT_ARGB : originalImage.getType();
resizedImage = CommonUtilities.resizeImage(originalImage, IMG_HEIGHT, IMG_WIDTH);
ImageIO.write(resizedImage, "jpg", new File(path + "/"+ testCaseId + "/img/" + index + ".jpg"));
Image jpeg = Image.getInstance(path + "/" + testCaseId + "/img/"+ index + ".jpg");
Step 2:
BufferedImage pathforToast= original image;
Step 3:
System.setProperty("jna.library.path","C:/Users/Dell/workspace/MOBILEFRAMEWORK/dlls/x64/");
Tesseract instance = Tesseract.getInstance();
`enter code here`ImageIO.scanForPlugins();
String result=null;
result = instance.doOCR(pathforToast);`enter code here`
System.out.println(result);`enter code here`
Appium 1.6.4#beta latest version supports toast messages
Take screen shot of Toast Message page and try to convert the image file in to Text and verify the text using the below code.
public void imageconversion(String filePath) throws IOException,
{
ITesseract instance = new Tesseract();
//file path is the image which you need to convert to text
File imageFile = new File(filePath);
BufferedImage img = null;
img = ImageIO.read(imageFile);
BufferedImage blackNWhite = new BufferedImage(img.getWidth(),img.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
Graphics2D graphics = blackNWhite.createGraphics();
graphics.drawImage(img, 0, 0, null);
//path where your downloaded tessdata exists
instance.setDatapath("E://ocr//data");
//What language you required to convert,( e.g. English)
instance.setLanguage("eng");
String result = instance.doOCR(blackNWhite);
System.out.println(result);
}
Appium Directly does not give any API to read toast message we need to do it using tess4j jar. First we need to take screen shot and then we need to read the text from screen shot using tess4j API.
static String scrShotDir = "screenshots";
File scrFile;
static File scrShotDirPath = new java.io.File("./"+ scrShotDir+ "//");
String destFile;
static AndroidDriver driver = null;
public String readToastMessage() throws TesseractException {
String imgName = takeScreenShot();
String result = null;
File imageFile = new File(scrShotDirPath, imgName);
System.out.println("Image name is :" + imageFile.toString());
ITesseract instance = new Tesseract();
File tessDataFolder = LoadLibs.extractTessResources("tessdata"); // Extracts
// Tessdata
// folder
// from
// referenced
// tess4j
// jar
// for
// language
// support
instance.setDatapath(tessDataFolder.getAbsolutePath()); // sets tessData
// path
result = instance.doOCR(imageFile);
System.out.println(result);
return result;
}
/**
* Takes screenshot of active screen
*
* #return ImageFileName
*/
public String takeScreenShot() {
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy__hh_mm_ssaa");
new File(scrShotDir).mkdirs(); // Create folder under project with name
// "screenshots" if doesn't exist
destFile = dateFormat.format(new Date()) + ".png"; // Set file name
// using current
// date time.
try {
FileUtils.copyFile(scrFile, new File(scrShotDir + "/" + destFile)); // Copy
// paste
// file
// at
// destination
// folder
// location
} catch (IOException e) {
System.out.println("Image not transfered to screenshot folder");
e.printStackTrace();
}
return destFile;
}
For more details Refer this video - https://www.youtube.com/watch?v=lM6-ZFXiSls
I found three ways to capture a Toast message and verify them.
To get the page source and verify the toast message from it.
public void verifyToastMessageUsingPageSource(String toastmsg) throws InterruptedException {
boolean found = false;
for(int i =0 ; i <8; i++){
if(getDriver().getPageSource().contains("class=\"android.widget.Toast\" text=\""+toastmsg+"\"")){
found = true;
break;
}
Thread.sleep(300);
}
Assert.assertTrue(found,"toast message "+toastmsg+" is present");
}
Similar can be found using Xpath: //android.widget.Toast[1]
Using the grep command, wait for a toast message in uiautomator events.
run the command before clicking and toast message will be varified.
adb shell uiautomator events | grep "ToastMessgae"
This is tricky and needs more code to run.
start capturing screenshots thread.
perform click action
stop screen capturing thread.
extract text from the captured images using OCR and verify the toast message is present in the captured images.
I prefer the 1st and 2nd option, it provides validation in less time with less code.
comment if you need code for 2nd and 3rd point.
Appium with version number>=1.6.4 supports toast notification with UiAutomator2.
In Javascript with webdriver you can do like this
let toast=await driver1.elements("xpath","/hierarchy/android.widget.Toast");
let data=await toast[0].text();
console.log(data)

CefSharp.Wpf: Open csv, mailto and pdf with Process.Start(...)

I need to handle different content types from f:///. My application renders offline websites in a WPF application. Everything is working except for links to other content files (csv, mailto and pdf).
If I register a CefCustomScheme for "mailto", then I get the ProcessRequestAsync and can run the Process.Start(...). However another blank window also popup.
If I then add a second CefCustomScheme for "file", then nothing happens. None of the ISchemeHandler ProcessRequestAsync methods are invoked.
I must be able to handle all requests, excluding *.html, in a separate handler
Essentially I just want to replicate the behavior of the MS Web-browser Control. There all I did was point to the entry page (index.htm), and everything loaded. Then if a user clicks any link, the control handled the action and started the correct process (content handler, i.e. Excel for Csv).
The code:
// Startup
var settings = new CefSettings();
settings.LogFile = #"c:\temp\ceflog.txt";
settings.LogSeverity = LogSeverity.Verbose;
settings.IgnoreCertificateErrors = true;
CefCustomScheme mailtoScheme = new CefCustomScheme();
mailtoScheme.SchemeName = "mailto";
mailtoScheme.SchemeHandlerFactory = new SchemeHandlerFactory();
CefCustomScheme filesScheme = new CefCustomScheme();
mailtoScheme.SchemeName = "file";
mailtoScheme.SchemeHandlerFactory = new SchemeHandlerFactory();
settings.RegisterScheme(mailtoScheme);
settings.RegisterScheme(filesScheme);
if (!Cef.Initialize(settings))
throw new InvalidOperationException("Failed to initialize the browser factory");
-- SchemeHandlerFactory
public class SchemeHandlerFactory : ISchemeHandlerFactory {
public ISchemeHandler Create() {
return new CustomSchemeHandler();
}
}
-- Handler
public class CustomSchemeHandler : ISchemeHandler {
private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public bool ProcessRequestAsync(IRequest request, ISchemeHandlerResponse response, OnRequestCompletedHandler requestCompletedCallback) {
_log.DebugFormat("Processing url: {0}", request.Dump());
var knownContentTypes = new[] {".csv", ".xsls", ".xlsx", ".pdf", ".txt"};
var ext=Path.GetExtension(request.Url);
if(knownContentTypes.Contains(ext)) {
_log.DebugFormat("Starting process for: {0}",request.Url);
Process.Start(request.Url);
return false;
}
return true;
}
The solution was to implement an IRequestHandler and use the OnBeforeResourceLoad event to check what content was requested. The ISchemeHandler is used for the "mailto" actions.
In my case I had to assign the request handler after the frame loaded. This allowed the web browser to render all content first.
Code sample GitHub example

How to write logs data in a local txt file?

I have an AIR-app and I want it to write down logs to a txt file in its application directory.
I have this class:
public class Logger
{
public static function log(message:String):void
{
var logFile:File = File.applicationDirectory;
logFile = logFile.resolvePath("log/Logs.txt");
var fileStream:FileStream = new FileStream();
fileStream.open(logFile, FileMode.APPEND);
fileStream.writeUTFBytes("\n" + message);
fileStream.close();
}
}
}
After installation my app has this file structure
log
--Logs.txt
META-INF
xml
airapp.exe
airapp.swf
mimetype
So the path I'm using is correct.
But no writes happen to the file!
Could you help me with that please?
Thanks
Upd. Ok, I'm stupid. I should have read documentation properly first.
You cannot edit data in application directory.
So I should use applicationStorageDirectory.
But how can I create a file in that directory once (on app installation) and then append logs in it?
Try the following....
function saveLog(logStr:String):void
{
var myFile:File = File.applicationStorageDirectory.resolvePath("my_logs.txt");
var fs:FileStream = new FileStream();
if(!myFile.exists){
fs.open(myFile, FileMode.WRITE);
}
else{
fs.open(myFile, FileMode.APPEND);
}
fs.writeUTFBytes("\n" + logStr);
fs.close();
}
call this function on creation complete and then from where to add logs.
If you want the real log than you should use third party installer rather than native run time of adobe air. Try Wixedit(opensource) or install aware .
You have to export your project into captive run time format that is provided in flash builder.
try this link
http://www.adobe.com/devnet/air/articles/customize-setup-for-AIR-app-with-captive-runtime.html
Try something like:
function saveLog(content:String):void
{
var data:ByteArray = new ByteArray();
data.writeMultiByte(content, "utf-8");
file.save(data, "Logs.txt");
}
saveLog('testing :)');
To open and edit your log file, try to follow this tutorial

Sharepoint 2010 Upload file using Silverlight 4.0

I am trying to do a file upload from Silverlight(Client Object Model) to Sharepoint 2010 library.. Please see the code below..
try{
context = new ClientContext("http://deepu-pc/");
web = context.Web;
context.Load(web);
OpenFileDialog oFileDialog = new OpenFileDialog();
oFileDialog.FilterIndex = 1;
oFileDialog.Multiselect = false;
if (oFileDialog.ShowDialog().Value == true)
{
var localFile = new FileCreationInformation();
localFile.Content = System.IO.File.ReadAllBytes(oFileDialog.File.FullName);
localFile.Url = System.IO.Path.GetFileName(oFileDialog.File.Name);
List docs = web.Lists.GetByTitle("Gallery");
context.Load(docs);
File file = docs.RootFolder.Files.Add(localFile);
context.Load(file);
context.ExecuteQueryAsync(OnSiteLoadSuccess, OnSiteLoadFailure);
}
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
}
But I am getting the following error
System.Security.SecurityException: File operation not permitted. Access to path '' is denied.
at System.IO.FileSecurityState.EnsureState()
at System.IO.FileSystemInfo.get_FullName()
at ImageUploadSilverlight.MainPage.FileUpload_Click(Object sender, RoutedEventArgs e)
Any help would be appreciated
Thanks
Deepu
Silverlight runs with very restricted access to the client user's filesystem. When using an open-file dialog, you can get the name of the selected file within its parent folder, the length of the file, and a stream from which to read the data in the file, but not much more than that. You can't read the full path of the file selected, and you are getting the exception because you are attempting to do precisely that.
If you want to read the entire content of the file into a byte array, you'll have to replace the line
localFile.Content = System.IO.File.ReadAllBytes(oFileDialog.File.FullName);
with something like
localFile.content = ReadFully(oFileDialog.File.OpenRead());
The ReadFully method reads the entire content of a stream into a byte array. It's not a standard Silverlight method; instead it
is taken from this answer. (I gave this method a quick test on Silverlight, and it appears to work.)

Resources