Save and read files in isolated storage - windows-phone-7

I try to save files in IsoStore. In WP8 emulator files have been successfully saved, but when I run my program in other emulators or on my phone(with WP7.8) I get a error: "path must be a valid file name"
I do this:
var path = #"\Shared\Media\mapp\";
var imageName = guid from the server;
if (!_fileStorage.DirectoryExists(path))
_fileStorage.CreateDirectory(path);
//here I get a error using (IsolatedStorageFileStream fileStream =
_fileStorage.OpenFile(path + imageName,
FileMode.OpenOrCreate))
{//do anything}
I try to set path = #"iso:\Shared\Media\mapp\" or #"isostore:\Shared\Media\mapp\" or #"files:\Shared\Media\mapp\" or #"file:\Shared\Media\mapp\" and it doesn't work.
If I set #"\Shared\Media\" all fine in all devices. Who can tell me why I can't create a directory?

For Windows-Phone-7 you can't create a directory, which name ends with "/" or "//", that will cause an "path must be a valid file name" error.
To solve your problem, just change your code a bit:
var path = #"\Shared\Media\mapp";
var imageName = guid from the server;
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!store.FileExists(path))
{
store.CreateDirectory(path);
}
store.OpenFile(path + "\\" + imageName, FileMode.OpenOrCreate);
}
Hope, that helps.

Related

How to get app name programatically in native script

I'm trying to save some files on my local File storage So, I'm doing Something like below
var folder_name = "abcde/" + viewModel.dir_path;
const documents = fileSystemModule.knownFolders.documents();
documents._path = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
const folder = documents.getFolder(folder_name);
var file = fileSystemModule.path.join(folder._path, this.pdf_url.split("/").pop());
var url = this.pdf_url;
httpModule.getFile(url, file).then(function(result) {
console.log(result);
Toast.makeText(`${result._name} is succesfully downloaded in ${folder_name}`).show();
}, function(e) {
console.log(e);
});
the only problem is the hardcoded value abcde/ I want it to be app name. whatever the app name is it should take that name.
I don't find any ways to read app name programatically. I need this to Android I'm not interested in IOS.
may be this is one could be the answer. but still this is not a proper way
const documents = fileSystemModule.knownFolders.currentApp();
console.log(documents); --> "/data/data/org.nativescript.app_name/files/app"
var str = documents;
var arr = str.split("/")[3];
console.log(arr.split(".")[2]); ---> app_name
In Android, you can set the app name for the application in strings.xml, for example:
<string name="app_name">"App_Name"</string>
Then whenever you want to reuse the app name, you can use the getString method with its name in the application.

Nativescript: iOS Utils openFile

I am working on code to open files which have been downloaded from the app, and I am using the utils.ios.openFile function. I can get the files to display on screen, however I am also getting the following warning on the console. Has anyone run into this, or have any ideas on how to resolve?
Unbalanced calls to begin/end appearance transitions for
. Cannot find
preview item for proxy: -
mobile-application.png (0)
The following is the code I am using:
var documents = fs.knownFolders.documents();
filePath = fs.path.join(documents.path, filename);
if(fs.File.exists(filePath)){
utilModule.ios.openFile(filePath)
.catch(function(error){
});
} else {
return Promise.reject(new Error("File not Found"));
}
You could set the filePath to the image, without using openFile(filePath) method. You could review the below-attached example.
let folder = fs.knownFolders.documents();
let path = fs.path.join(folder.path, "Test.png");
if(fs.File.exists(path )){
image.src=path
}

sub folder in not creating on azure in asp.net mvc5 application

I am ruing my website on azure, every folder is present on site directory in azure but uploadimages is my sub folder of content is absent from wwwroot, and images is not uploading also
I am using
var path =Path.Combine(Server.MapPath("~/Content/UploadImages/")+filename);
same with document upload
According to your description, I have tested on my side, please follow below to find out whether it could help you.
As you said, you got the target file path by this code:
var path = Path.Combine(Server.MapPath("~/Content/UploadImages/") + filename);
Before uploading files, please make sure that the directory in your web server “~/Content/UploadImages/” is existed.
Here is my test code:
MVC controller method
[HttpPost]
public JsonResult UploadFiles()
{
try
{
foreach (string file in Request.Files)
{
var fileContent = Request.Files[file];
if (fileContent != null && fileContent.ContentLength > 0)
{
var stream = fileContent.InputStream;
var fileName = Path.GetFileName(fileContent.FileName);
string baseDir = Server.MapPath("~/Content/UploadImages/");
if (!Directory.Exists(baseDir))
Directory.CreateDirectory(baseDir);
var path = Path.Combine(baseDir, fileName);
using (var fileStream = System.IO.File.Create(path))
{
stream.CopyTo(fileStream);
}
}
}
}
catch (Exception e)
{
return Json(new
{
Flag = false,
Message = string.Format("File Uploaded failed with exception:{0}", e.Message)
});
}
return Json(new
{
Flag = true,
Message = "File uploaded successfully!"
});
}
Additionally, for long-term consideration, you could store your files on Azure Blob Storage which could bring you some benefits, such as:
1.Serving images or documents directly to a browser
2.Storing files for distributed access
3.When you scale up your site, your site could run in multiple Web Server instances which could access the same files & docs simultaneously
For more details, please refer to this link: https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/

How to include resources to application for windows phone?

I have a problem: I created new c# project for windows phone (in VS 2013) and set test file property as "Copy if newer", but I cannot see file in emulator's Local folder. What do I do wrong?
More detailed:
Create app:
File->New->Project->Templates->Visual C#->Store Apps->Windows Phone Apps->Blank App (Windows Phone)
set test file property
run on emulator (there is a button for this) and list files with code:
async void listFolder()
{
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
Stack<StorageFolder> stack = new Stack<StorageFolder>();
stack.Push(local);
StorageFolder current;
string path;
byte[] bytes;
StorageFile logFile = await local.CreateFileAsync("log.txt", CreationCollisionOption.ReplaceExisting);
using (var s = await logFile.OpenStreamForWriteAsync())
{
while (stack.Count > 0)
{
current = stack.Pop();
foreach (StorageFolder f in await current.GetFoldersAsync())
{
stack.Push(f);
}
path = current.Path;
bytes = Encoding.UTF8.GetBytes(current.Path + "\n");
s.Write(bytes, 0, bytes.Length);
foreach (StorageFile f in await current.GetFilesAsync())
{
bytes = Encoding.UTF8.GetBytes(f.Path + "\n");
s.Write(bytes, 0, bytes.Length);
}
s.Flush();
}
}
}
Check file with Windows Phone Power Tools. Local folder contains log.txt only. Log contains Local directory and log file. No TestText.txt
How do I include file to application and access it on emulator?
Limitations:
I do need to held data on local storage (no web links, no cloud)
If you want to access files that come with your package, then you need to use Package.InstalledLocation, you won't find those files in ApplicationData.LocalFolder.
Note that files included in Package are read-only and you won't be able to write them.
Some more information you will also find at this answer.

mvc3 ImageResizer

I downloaded the Nugent ImageResizer and I am trying to resize a picture on upload following an example on this page http://imageresizing.net/docs/managed but I can't seen to put this in a Var or Image variable so i can see it in the Path.Combine here is the code
var fileName = Path.GetFileName(file.FileName);
var changename = getid + "_" + fileName;
ImageBuilder.Current.Build(changename, changename,
new ResizeSettings("width=130&height=130"));
var path = Path.Combine(Server.MapPath("~/uploads/profilepic"), changename);
file.SaveAs(path);
How can I get the ImageBuilder inside a var or some type of image variable what i would like to do is something like this
var resized= ImageBuilder.Current.Build(changename, changename,
new ResizeSettings("width=130&height=130"));
var path = Path.Combine(Server.MapPath("~/uploads/profilepic"), resized);
file.SaveAs(path);
all that im trying to do is put the ImageBuilder inside the Path.Combine without getting an error, any help would be appreciated .
ImageResizer should be given the uploaded file and the output path directly
ImageResizer supports both GUIDs and path sanitization. NEVER use the uploaded filename as-is!
var i = new ImageJob(file,
"~/uploads/profilepic/<guid>_<filename:A-Za-z0-9>.<ext>",
new ResizeSettings("width=130&height=130&format=jpg"));
i.CreateParentDirectory = true; //Auto-create the uploads directory.
i.Build();
var newVirtualPath = ImageResizer.Util.PathUtils.GuessVirtualPath(i.FinalPath);

Resources