I want to use the file in isolated storage using its full path(like URI), I know there is sandboxed api to access isolated storage. But i have to load the images using their paths.
So is it possible ?
I got it like this:
public string GetAbsolutePathOfFile(string filePath)
{
using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var isfs = storage.OpenFile(filePath, FileMode.Open, FileAccess.ReadWrite,
FileShare.ReadWrite))
{
return isfs.Name; // this return the absolute path.
}
}
}
Related
I'm creating a POC to store files in Azure following the steps in https://learn.microsoft.com/en-us/azure/storage/blobs/data-lake-storage-directory-file-acl-dotnet. In the snippet below creating the directory fails with message No such host is known. (securedfstest02.blob.core.windows.net:443). Appreciate any suggestion to workaround
this issue.
using Azure;
using Azure.Storage;
using Azure.Storage.Files.DataLake;
using Azure.Storage.Files.DataLake.Models;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace DataLakeHelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
try
{
CreateFileClientAsync_DirectoryAsync().Wait();
}
catch(Exception e)
{
Console.WriteLine(e);
}
}
static async Task CreateFileClientAsync_DirectoryAsync()
{
// Make StorageSharedKeyCredential to pass to the serviceClient
string storageAccountName = "secureblobtest02";
string storageAccountKey = "mykeyredacted";
string dfsUri = "https://" + storageAccountName + ".dfs.core.windows.net";
StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(storageAccountName, storageAccountKey);
// Create DataLakeServiceClient using StorageSharedKeyCredentials
DataLakeServiceClient serviceClient = new DataLakeServiceClient(new Uri(dfsUri), sharedKeyCredential);
// Create a DataLake Filesystem
DataLakeFileSystemClient filesystem = serviceClient.GetFileSystemClient("my-filesystem");
if(!await filesystem.ExistsAsync())
await filesystem.CreateAsync();
//Create a DataLake Directory
DataLakeDirectoryClient directory = filesystem.CreateDirectory("my-dir");
if (!await directory.ExistsAsync())
await directory.CreateAsync();
// Create a DataLake File using a DataLake Directory
DataLakeFileClient file = directory.GetFileClient("my-file");
if(!await file.ExistsAsync())
await file.CreateAsync();
// Verify we created one file
var response = filesystem.GetPathsAsync();
IAsyncEnumerator<PathItem> enumerator = response.GetAsyncEnumerator();
Console.WriteLine(enumerator?.Current?.Name);
// Cleanup
await filesystem.DeleteAsync();
}
}
}
--Update
In your question, you mention of Azure data lake, but you seem to have the host: securedfstest02.blob.core.windows.net
Azure Data Lake Storage uses .dfs.core.windows.net/ whereas a Azure Blob Storage uses .blob.core.windows.net/ While using Blob service related operations in ADLS you would have to change the endpoint too accordingly.
please note the official MS docs URI templates.
I have used the same code and was able to create directory. Just replaced my adls credentials. I have not configured any additional permissions. ADLS is Allowed access from all networks. You might want to check if yours is by default configured to specific network or if firewall allows client (your) IP.
using Azure;
using Azure.Storage;
using Azure.Storage.Files.DataLake;
using Azure.Storage.Files.DataLake.Models;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace DataLakeHelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Starting....");
try
{
Console.WriteLine("Executing...");
CreateFileClientAsync_DirectoryAsync().Wait();
Console.WriteLine("Done");
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
static async Task CreateFileClientAsync_DirectoryAsync()
{
// Make StorageSharedKeyCredential to pass to the serviceClient
string storageAccountName = "kteststarageeadls";
string storageAccountKey = "6fAe+P8LRe8LH0Ahxxxxxxxxx5ma17Slr7SjLy4oVYSgj05m+zWZuy5X8p4/Bbxxx8efzCj/X+On/Fwmxxxo7g==";
string dfsUri = "https://" + "kteststarageeadls" + ".dfs.core.windows.net";
StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(storageAccountName, storageAccountKey);
// Create DataLakeServiceClient using StorageSharedKeyCredentials
DataLakeServiceClient serviceClient = new DataLakeServiceClient(new Uri(dfsUri), sharedKeyCredential);
// Create a DataLake Filesystem
DataLakeFileSystemClient filesystem = serviceClient.GetFileSystemClient("my-filesystem");
if (!await filesystem.ExistsAsync())
await filesystem.CreateAsync();
//Create a DataLake Directory
DataLakeDirectoryClient directory = filesystem.CreateDirectory("my-dir");
if (!await directory.ExistsAsync())
await directory.CreateAsync();
// Create a DataLake File using a DataLake Directory
DataLakeFileClient file = directory.GetFileClient("my-file");
if (!await file.ExistsAsync())
await file.CreateAsync();
// Verify we created one file
var response = filesystem.GetPathsAsync();
IAsyncEnumerator<PathItem> enumerator = response.GetAsyncEnumerator();
Console.WriteLine(enumerator?.Current?.Name);
// Cleanup
//await filesystem.DeleteAsync();
}
}
}
I've edited storage account key, used for reference only.
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/
I have a class that defines my level. I have an XML file in my Content Project that stores the details of the level.
I can load the details of the XML file from the Content Project with LoadContent(). This creates an object with all the details from the XML file.
Now, I want to save that game level into isolated storage.
All the examples that I've seen, indicate that I need to use XMLWriter and XMLSerializer. Why is that? Can I not use the mechanism that the XNA framework uses to load from the Content Pipeline?
You don't need an XMLWriter or XMLSerializer, but you do need a serializer.
Below is an example of a my Generic IsolatedStorage Utilty
public static void Save<T>(string fileName, T item)
{
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(fileName, FileMode.Create, storage))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
serializer.WriteObject(fileStream, item);
}
}
}
public static T Load<T>(string fileName)
{
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(fileName, FileMode.Open, storage))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
return (T)serializer.ReadObject(fileStream);
}
}
}
When YOU reference the XML as an XNA content it is compiled throught the ContentPipeline. So when you load the Content you do it through the ContentManager. This XML file referenced should NOT be in the ContentPipeline because then it cannot be modified. You should leave Static files referenced through the ContentPipline and leave all Dynamic files saved in IsolatedStorage. Once files are comiled they cannot be changed thats why it cant be saved to the ContentPipeline.
I want to read a binary file using BinaryReader, but I keep getting an exception:
using (var stream = File.Open("file.bin", FileMode.Open, FileAccess.Read))
{
using (BinaryReader r = new BinaryReader(stream)) //EXCEPTION
{
}
}
the "file.bin" has been set as a Content in the build action, but I keep getting this exception:
System.MethodAccessException was unhandled
Attempt to access the method failed: System.IO.File.Open(System.String, System.IO.FileMode, System.IO.FileAccess)
You don't use File.Open on Windows Phone 7 - you have to use isolated storage.
See the System.IO.IsolatedStorage namespace for more details.
For example:
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var stream = store.OpenFile("file.bin", FileMode.Open))
{
using (var reader = new BinaryReader(stream))
{
}
}
}
EDIT: As noted in comments, for content built into the XAP, you should use Application.GetResourceStream.
I was trying to save multiple images into isolated storage by using creating a imageFolder in isolated storage and storing all my images inside.But it have an error so please anyone could help me solve the error or got other method way help me thanks.IF possible I would appreciate if you guys can show me your code that works. Actually my code would like to be under a button event handler.Thanks And the error is :Operation not permitted on IsolatedStorageFileStream.
My Code :
private void SaveToLocalStorage(string imageFolder, string imageFileName)
{
imageFileName = name.Text;
MessageBox.Show(imageFileName);
var isf = IsolatedStorageFile.GetUserStoreForApplication();
if (isf.DirectoryExists(imageFolder))
{
isf.CreateDirectory(imageFolder);
}
string filePath = Path.Combine(imageFolder, imageFileName);
MessageBox.Show(filePath);
using (var stream = isf.CreateFile(filePath))
{
var bmp= new WriteableBitmap(inkCanvas, inkCanvas.RenderTransform);
bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
}
}
First off, you probably want to be creating the directory if it DOESN'T exist, not if it does:
if (!isf.DirectoryExists(imageFolder))
{
isf.CreateDirectory(imageFolder);
}