Is it possible to save an image from an url in the assets folder?
void DataPacking::createAndSaveImage(QString argSavingFilePath,
QByteArray argDataLoaded) {
m_file = new QFile;
m_file->setFileName(argSavingFilePath);
m_file->open(QIODevice::WriteOnly);
m_file->write(argDataLoaded);
m_file->close();
m_file->~QFile();
}
m_savingFilePath = QDir::homePath() + "app/native/assets/images/"
+ QString("multipleActive.png");
createAndSaveImage(m_savingFilePath, m_dataLoaded);
but when I try to use this image, I am getting the error below.
"Unable to get asset in (/apps/com.bluewave.LeasePlan.testDev_e_LeasePlan45b0f435/native/assets/): (/images/multipleActive.png)."
The assets directory (or more properly the app directory) is part of the protected area of the application sandbox that can not be changed. If you want to store data in the sandbox you should use the data directory.
See: https://developer.blackberry.com/native/documentation/cascades/device_platform/data_access/file_system.html
Related
I was following a small tutorial of Microsoft.
Which basically saves your text input onto the internal memory of your device.
String _filename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Notes.txt");
Results in: /data/user/0/com.companyname.notes/files/.local/share/Notes.txt for me.
Now, while everything works, I would like to see this Notes.txt file in the folder.
I have searched far and wide, but can't seem to find a way to locate this file on my device.
I can go to Android/data/com.companyname.notes/files but then I only see a ._override_ folder with the app project files in it, but without the Notes.txt
Any ideas?
Thanks
From your path:/data/user/0/com.companyname.notes/files/.local/share/Notes.txt, we can know that you want to access internal storage, but Internal storage refers to the non-volatile memory that Android allocates to the operating system, APKs, and for individual apps. This space is not accessible except by the operating system or apps. So you can not find this text file from internal storage.
If you want to see file, you can save this file in external storage
/storage/emulated/0/Android/data/com.companyname.app/files
More detailed info about internal storage, see:
https://learn.microsoft.com/en-us/xamarin/android/platform/files/
Update
If you want to save text file, you should declare one of the two permissions for external storage in the AndroidManifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Then the primary location for private external files is found by calling the method Android.Content.Context.GetExternalFilesDir(string type). This method will return a Java.IO.File object that represents the private external storage directory for the app. Passing null to this method will return the path to the user's storage directory for the application. As an example, for an application with the package name com.companyname.app, the "root" directory of the private external files would be:
/storage/emulated/0/Android/data/com.companyname.app/files/
In the Forms, you need to create new interface:
public interface IFileSystem
{
string GetExternalStorage();
}
Implement this interface in Android:
[assembly: Dependency(typeof(FileSystemImplementation))]
namespace FileApp.Droid
{
public class FileSystemImplementation : IFileSystem
{
public string GetExternalStorage()
{
Context context = Android.App.Application.Context;
var filePath = context.GetExternalFilesDir("");
return filePath.Path;
}
}
}
Now you can create text file and save text in this file:
private async void Btn1_Clicked(object sender, EventArgs e)
{
var folderPath = DependencyService.Get<IFileSystem>().GetExternalStorage();
var file = Path.Combine(folderPath, "count.txt");
using (var writer = File.CreateText(file))
{
await writer.WriteLineAsync("123456789000000000000000000000000000000000000");
}
}
I have made a sample:
https://github.com/CherryBu/FileApp
The exact path to the private external storage directory can vary from device to device and between versions of Android.
Open your File Manager App
Go to Android/data
Find the .com folder, in your case, com.companyname.notes
Follow the path until you find the file
I have written a code to upload the images(profile picture of an student) in the server running in linux environment.The code is shown below
#RequestMapping(value = "/updatePhoto",method = RequestMethod.POST)
public String handleFormUpload(#RequestParam("id") String id,
#RequestParam("file") MultipartFile file,
HttpServletRequest request,
Model model) throws IOException {
if(!file.isEmpty())
{
try
{
String relativePath="/resources";
String absolutePath=request.getServletContext().getRealPath(relativePath);
System.out.print(absolutePath);
byte[] bytes=file.getBytes();
File dir=new File(absolutePath);
if(!dir.exists())
{
dir.mkdir();
}
File uploadFile=new File(dir.getAbsolutePath()+File.separator+id+".jpg");
BufferedOutputStream outputStream=new BufferedOutputStream(new FileOutputStream(uploadFile));
outputStream.write(bytes);
outputStream.close();
model.addAttribute("uploadMessage","image uploaded for id"+id);
}
catch (Exception e)
{
System.out.print(e);
}
}
return "successFileUpload";
}
i have stored in "/resources" folder.but the problem is, whenever i generate the war file of whole application and deploy in server, it flushes the "/resources" folder and deletes the old uploaded images.Is there any way or the path ,i could upload the images.
The way I do is:
Create a directory in the server. For example: /myImages
Then grant full permissions for tomcat user
You are good to go now. I have read somewhere that you shouldn't save your stuff on /resources folder because it makes your app independent from container you are using: with tomcat you could use catalina.home but what if you shift to another container
I store the images inside my Tomcat home location as it will be outside of my project folder(war) and inside the tomcat.
String rootPath = System.getProperty("catalina.home");
File dir = new File(rootPath + File.separator + "images");
The above lines of code will create a folder in tomcat base directory with name 'images'.
This is the one of the best ways to store images.
Here's simple way
System.out.println(System.getProperty("user.dir"));
It is fairly common to allow users to download a file via having some path modifier in the URL
//MVC Action to download the correct file From our Content directory
public ActionResult GetFile(string name) {
string path = this.Server.MapPath("~/Content/" + name);
byte[] file = System.IO.File.ReadAllBytes(path);
return this.File(file, "html/text");
}
quoted from http://hugoware.net/blog/dude-for-real-encrypt-your-web-config
An application I'm working with has liberal path downloads ( directory based ) sprinkled throughout the application, hence it is super vulnerable to requests like "http://localhost:1100/Home/GetFile?name=../web.config" or ( ..%2fweb.config )
Is there an easy way to restrict access to the config file - do I need to provide a custom Server.MapPath with whitelisted directories - or is there a better way.
How do you secure your file downloads - are path based downloads inherently insecure?
A simple option, assuming that all files in the ~/Content directory are safe to download would be to verify that the path is actually under (or in) the ~/Content directory and not up from it, as ~/Content/../web.config would be. I might do something like this:
// MVC Action to download the correct file From our Content directory
public ActionResult GetFile(string name) {
// Safe path
var safePath = this.Server.MapPath("~/Content");
// Requested path
string path = this.Server.MapPath("~/Content/" + name);
// Make sure requested path is safe
if (!path.StartsWith(safePath))
// NOT SAFE! Do something here, like show an error message
// Read file and return it
byte[] file = System.IO.File.ReadAllBytes(path);
return this.File(file, "html/text");
}
I am working on website in which i want to copy the file from my application folder to other folder on same server (But this folder is out of my application folder i.e. my application on C driver and the destination folder is on D drive).Is this possible using any functionality of Asp.Net?
Thanks in advance.
YES it's possible, the only concern that you have to watch for is that the CopyTo path should be the full path, not the relative one (ex: c:\websites\myOtherFolder).
this way, you can successfully copy/move the file from your ASP.NET code.
below is a pseudo code to show you how to get it done (assuming that the file has been placed on the root folder of your ASP.NET Application).
using System.IO;
..
..
..
// Get the current app path:
var currentApplicationPath = HttpContext.Current.Request.PhysicalApplicationPath;
//Get the full path of the file
var fullFilePath = currentApplicationPath + fileNameWithExtension;
// Get the destination path
var copyToPath = "This has to be the full path to your destination directory.
Example d:\myfolder";
// Copy the file
File.Copy(fullFilePath , copyToPath );
use this function:
System.IO.File.Copy(FileToCopy, NewCopy)
It's very easy to move file from one folder to other folder. you can change the file name while moving...
string Tranfiles, ProcessedFiles;
//Tranfiles = Server.MapPath(#"~\godurian\sth100\transfiles\" + Filename);
Tranfiles = Server.MapPath(#"~\transfiles\" + Filename);
if (File.Exists(Server.MapPath(#"~\transfiles\" + Filename)))
{
File.Delete(Server.MapPath(#"~\transfiles\" + Filename));
}
//ProcessedFiles = Server.MapPath(#"~\godurian\sth100\ProcessedFiles");
ProcessedFiles = Server.MapPath(#"~\ProcessedFiles");
File.Move(Tranfiles, ProcessedFiles);
That's it now you can check your application folder to confirm the move process status
I'll ONLY recieve an "Error #3000: Illegal path name" if I try to open a file which is placed inside the app-folder of the air. If the file is somewhere else outside of the app-folder it works.
private var file:File = File.documentsDirectory;
public function download():void{
var pdfFilter:FileFilter = new FileFilter("PDF Files", "*.pdf");
file.browseForOpen("Open", [pdfFilter]);
file.addEventListener(Event.SELECT, fileSelected);
}
private function fileSelected(e:Event):void
{
var destination:File = File.applicationDirectory
destination = destination.resolvePath("test.pdf");
/*
//This works, also if the file to copy is placed inside the appfolder
file.copyTo(destination, true);
*/
/*This Throws me an Error #3000, but ONLY if the file is located in
the App folder*/
file.openWithDefaultApplication();
}
When i try to get the same file and copy it to another place it's doing fine.
Why that? Something special to do if i wanna open files which are inside the appfolder?
It also don't work in debug mode - bin-debug.
Regards, Temo
After reading the document a few times i saw that this is not possible (it's not a bug, it's a feature!?!)
Opening files with the default system application
You cannot use the openWithDefaultApplication() method with files located in the application directory.
So I do this instead:
file.copyTo(tempFile);
tempFile.openWithDefaultApplication();
Not so nice, but it works.