Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdnichollsc/619f9db2c3e61667ea35 to your computer and use it in GitHub Desktop.
Save jdnichollsc/619f9db2c3e61667ea35 to your computer and use it in GitHub Desktop.
var file = fileOrFileArray[i];
if (file.indexOf("data:application") >= 0) {
var fileData = Windows.Security.Cryptography.CryptographicBuffer.decodeFromBase64String(file.split(',')[1]);
var memoryStream = new Windows.Storage.Streams.InMemoryRandomAccessStream();
var dataWriter = new Windows.Storage.Streams.DataWriter(memoryStream);
dataWriter.writeBuffer(fileData);
dataWriter.storeAsync().done(function () {
dataWriter.flushAsync().done(function () {
var fileStream = dataWriter.detachStream();
fileStream.seek(0);
var streamReference = Windows.Storage.Streams.RandomAccessStreamReference.createFromStream(fileStream);
//TODO...
e.request.data.setBitmap(streamReference); //Attach the file without the file name, remove this!
//We need to convert the stream to IStorageItem to use the following code
//storageItems.push(storageItem);
if (!--filesCount) {
storageItems.length && e.request.data.setStorageItems(storageItems);
deferral.complete();
}
});
});
}
//Text Message
var message = args[0];
//Title
var subject = args[1];
//File(s) Path
var fileOrFileArray = args[2];
//Web link
var url = args[3];
var folder = Windows.Storage.ApplicationData.current.temporaryFolder;
var getExtension = function (strBase64) {
return strBase64.substring(strBase64.indexOf("/") + 1, strBase64.indexOf(";base64"));
};
var replaceAll = function (str, find, replace) {
return str.replace(new RegExp(find, 'g'), replace);
};
var sanitizeFilename = function (name) {
return replaceAll(name, "[:\\\\/*?|<> ]", "_");
};
var getFileName = function (position, fileExtension) {
var fileName = (subject ? sanitizeFilename(subject) : "file") + (position == 0 ? "" : "_" + position) + "." + fileExtension;
return fileName;
};
var createTemporalFile = function (fileName, buffer) {
var filePath = "";
return folder.createFileAsync(fileName, Windows.Storage.CreationCollisionOption.replaceExisting).then(function (file) {
filePath = file.path;
return Windows.Storage.FileIO.writeBufferAsync(file, buffer);
}).then(function(){
return Windows.Storage.StorageFile.getFileFromPathAsync(filePath);
});
};
var doShare = function (e) {
e.request.data.properties.title = subject?subject: "Sharing";
if (message) e.request.data.setText(message);
if (url) e.request.data.setWebLink(new Windows.Foundation.Uri(url));
if (fileOrFileArray.length > 0) {
var deferral = e.request.getDeferral();
var storageItems = [];
var filesCount = fileOrFileArray.length;
var completeFile = function () {
if (!--filesCount) {
storageItems.length && e.request.data.setStorageItems(storageItems);
deferral.complete();
}
};
for (var i = 0; i < fileOrFileArray.length; i++) {
var file = fileOrFileArray[i];
if (file.indexOf("data:") >= 0) {
var fileName = getFileName(i, getExtension(file));
var buffer = Windows.Security.Cryptography.CryptographicBuffer.decodeFromBase64String(file.split(',')[1]);
if (buffer) {
createTemporalFile(fileName, buffer).done(
function (file) {
storageItems.push(file);
completeFile();
},
function () {
completeFile();
}
);
}
else {
completeFile();
}
}
else {
Windows.Storage.StorageFile.getFileFromPathAsync(file).done(
function (file) {
storageItems.push(file);
completeFile();
},
function () {
completeFile();
}
);
}
}
}
};
@damirarh
Copy link

You could always save the stream to a temporary file in ApplicationData.TemporaryFolder and open that as an IStorageItem afterwards to pass it forward. I don't have the time to create a sample at the moment, but it shouldn't be too difficult to make it work.

@jdnichollsc
Copy link
Author

Hi @damirarh, thanks for your help! Exist any example to save the stream to a temporary file? ✌️ PD: I'm checking this :)

@jdnichollsc
Copy link
Author

Done! Thanks for all! 👯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment