Writing from persistent data path to streaming asset - file

I'm trying to get bytes from Application.persistentDataPath to streaming asset and save it as an mp4 file but my code doesnt work.
Here it is :
IEnumerator streamit(){
string filePath ="file:///"+ Path.Combine(Application.persistentDataPath, "Locomotive Part 1.mp4");
Uri uri = new Uri(filePath);
string converted = uri.AbsoluteUri;
WWW www = new WWW(converted);
Debug.Log (www.error);
yield return www;
using (FileStream fs = new FileStream(Application.persistentDataPath+"/example.mp4", FileMode.Append))
foreach(byte data in www.bytes){
fs.WriteByte (data);
}
}
So what am I doing wrong ? Its not working on both IOS and Mac

Oh, I found my problem.
Everything that doesn't work because of empty spaces on the source path. In IOS, you have to change empty spaces with "%20" whenever you are sending a path.
Thanks Anyway !

Related

How to show byte array(byte[]) as file(pdf/doc) in codenameone?

I am getting response from service as byte array(byte[]). in my app i want to show the file(pdf/doc) in app itself. is it possible? is there any way to save and view the file in codenameone?
Please try this code
String newString = String(byte[] bytes);
For PDF you need to create a file and open it using the native viewer:
FileSystemStorage fs = FileSystemStorage.getInstance();
String fileName = fs.getAppHomePath() + "myPDF.pdf";
try(OutputStream os = fs.openOutputStream(fileName)) {
os.write(myByteArrayData);
}
Display.getInstance().execute(fileName);

Save file to device with memorystream

I was wondering if it is possible with xamarin.forms to download any type of file to the device.. de files are stored on Azure, i get a Memorystream of the file, its very important for my app. my question excists of 2 parts actually,
how to download de file to the device of the user?,
and how to show the file of Any type in a default application of the type ( like pdf reader)
this is what i tried
MemoryStream memoryStream = AzureDownloader.DownloadFromAzureBlobStorage(null, doc.azure_container, doc.file_path, ref filen, true);
string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string localFilename = doc.filename;
string localPath = Path.Combine(documentsPath, localFilename);
File.WriteAllBytes(localPath, memoryStream.ToArray()); // this is a try to save to local storage
any help appreciated
how to download the file to the device of the user?
Use PCLStorage as its cross-platform and would work for iOS and Android:
public async Task CreateRealFileAsync()
{
// get hold of the file system
IFolder rootFolder = FileSystem.Current.LocalStorage;
// create a folder, if one does not exist already
IFolder folder = await rootFolder.CreateFolderAsync("MySubFolder", CreationCollisionOption.OpenIfExists);
// create a file, overwriting any existing file
IFile file = await folder.CreateFileAsync("MyFile.txt", CreationCollisionOption.ReplaceExisting);
// populate the file with some text
await file.WriteAllTextAsync("Sample Text...");
}
how to show the file of Any type in a default application of the type
( like pdf reader)
this is too broad and could have several solutions depending on what exactly you want to achieve.
Hope this helps

how to download an attachment in browser?

I'm using the Gmail API in browser and want to allow the user to download email attachments. I see https://developers.google.com/gmail/api/v1/reference/users/messages/attachments/get but it returns JSON and base64 data. I don't think I can get that data in memory then trigger a "download" to save the file locally. Even if I could I don't think it would be efficient - it would probably download the file in memory vs. streaming it to a file. I think I need a direct link to a file that returns the correct file name and raw binary data (not base64). Is there a way to do this? Right now the only way I see is to proxy requests.
You can get the data from the base64 and save it to file locally.
If you are getting the attachment in Java, you can use the FileOutputStream class (or f.write() in Python) to write the bytes to file and save it locally with a path.
You can try with the following sample code from Google Developer page:
public static void getAttachments(Gmail service, String userId, String messageId)
throws IOException {
Message message = service.users().messages().get(userId, messageId).execute();
List<MessagePart> parts = message.getPayload().getParts();
for (MessagePart part : parts) {
if (part.getFilename() != null && part.getFilename().length() > 0) {
String filename = part.getFilename();
String attId = part.getBody().getAttachmentId();
MessagePartBody attachPart = service.users().messages().attachment().
get(userId, messageId, attId).execute();
byte[] fileByteArray = Base64.decodeBase64(attachPart.getData());
FileOutputStream fileOutFile =
new FileOutputStream("directory_to_store_attachments" + filename);
fileOutFile.write(fileByteArray);
fileOutFile.close();
}
}
}

ADF: How to get path of file when using InputFile Component

I am using jdeveloper version 11.1.1.5.0. In my use case I have created Mail Client Send Mail program where I used ADF InputFile component to attach File on mail.
But problem is that InputFile Component only return path of file(only get file name). And in my mail program DataSource class use full path to access file name.
UploadedFile uploadfile=(UploadedFile) actionEvent.getNewValue();
String fname= uploadfile.getFilename();//this line only get file name.
So how can I get full path using adf InputFile component or any other way to fulfill my requirement.
You could save the uploaded file in a path at the server. Only take care about naming that file, because of concurrency of users you should follow a policy about it, for example, adding te time in milliseconds to the name of the file. Like this...
private String writeToFile(UploadedFile file) {
ServletContext servletCtx =
(ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext();
String fileDirPath = servletCtx.getRealPath("/files/tmp");
String fileName = getTimeInMilis()+file.getFilename();
try {
InputStream is = file.getInputStream();
OutputStream os =
new FileOutputStream(fileDirPath + "/"+fileName);
int readData;
while ((readData = is.read()) != -1) {
os.write(readData);
}
is.close();
os.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return fileName;
}
This method also returns the new name of the uploaded file. You can replace getTimeInMilis() with any naming policy you like.
It would be a security issue if a web app is able to see anything other than the data stream for an uploaded file. The directory structure of the client would not be exposed to the webapp. As such, unless you plan to upload the file from the same host as the server, you will not have access to the file path on the client.
Note: Using answer instead of comment due to reputation threshold

Weird behaviour of FileStream in WinForms

I have a WinForms application that checks for a TXT file in the application directory. There will be only a single line (user's email) or none. the code is like this:
public static string GetUserEmail()
{
string path = Application.StartupPath + "\\mail.txt";
MessageBox.Show(path);
string adres = String.Empty;
if (File.Exists(path))
{
using (StreamReader sr = new StreamReader(path))
{
adres = sr.ReadLine();
}
}
else
{
using (FileStream fs = File.Create(path))
{
using (StreamReader sr = new StreamReader(path))
{
adres = sr.ReadLine();
}
}
}
MessageBox.Show(adres);
return adres;
}
This Seems to work except one really weird behaviour. When I uninstall the program, and re-install, it still finds the file and reads the previous e-mail. I checked the ApplicationDirectory there is no such file, searched Windows, whole C drive, there is noı mail.txt but it still finds and read the mail address that I have entered in the very first installation. Thanks in advance for any help.
First of all, you are making use of Application.Startup. This will return the path where the executable file is present. So, there is no question of looking file to some other location. As you are saying that you searched entire C drive, it's really a strange issue.
I was facing similar issue with Windows Vista and Win 7. In these 2 operating systems, sometimes files get copied to "SysWow" folder as well.

Resources