Skip to content

Commit ce1f4a1

Browse files
acinaderflovilmart
authored andcommitted
Code sample on how to copy a url to Parse.File (#535)
1 parent fc85556 commit ce1f4a1

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

_includes/js/files.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ Parse will auto-detect the type of file you are uploading based on the file exte
2424
var file = new Parse.File("myfile.zzz", fileData, "image/png");
2525
</code></pre>
2626

27-
But most commonly for HTML5 apps, you'll want to use an html form with a file upload control. On modern browsers, this is easy. Create a file input tag which allows the user to pick a file from their local drive to upload:
27+
### Client Side
28+
In a browser, you'll want to use an html form with a file upload control. To do this, create a file input tag which allows the user to pick a file from their local drive to upload:
2829

2930
<pre><code>
3031
&lt;input type="file" id="profilePhotoFileUpload"&gt;
@@ -57,6 +58,33 @@ parseFile.save().then(function() {
5758
});
5859
</code></pre>
5960

61+
### Server Side
62+
In Cloud Code you can fetch images or other files and store them as a `Parse.File`.
63+
64+
```js
65+
const request = require('request-promise');
66+
const Parse = require('parse/node');
67+
68+
....
69+
70+
const options = {
71+
uri: 'https://bit.ly/2zD8fgm',
72+
resolveWithFullResponse: true,
73+
encoding: null, // <-- this is important for binary data like images.
74+
};
75+
76+
request(options)
77+
.then((response) => {
78+
const data = Array.from(Buffer.from(response.body, 'binary'));
79+
const contentType = response.headers['content-type'];
80+
const file = new Parse.File('logo', data, contentType);
81+
return file.save();
82+
})
83+
.then((file => console.log(file.url())))
84+
.catch(console.error);
85+
```
86+
87+
### Embedding files in other objects
6088
Finally, after the save completes, you can associate a `Parse.File` with a `Parse.Object` just like any other piece of data:
6189

6290
<pre><code class="javascript">
@@ -85,4 +113,4 @@ Parse.Cloud.httpRequest({ url: profilePhoto.url() }).then(function(response) {
85113

86114
You can delete files that are referenced by objects using the [REST API]({{ site.baseUrl }}/rest/guide/#deleting-files). You will need to provide the master key in order to be allowed to delete a file.
87115

88-
If your files are not referenced by any object in your app, it is not possible to delete them through the REST API. You may request a cleanup of unused files in your app's Settings page. Keep in mind that doing so may break functionality which depended on accessing unreferenced files through their URL property. Files that are currently associated with an object will not be affected.
116+
If your files are not referenced by any object in your app, it is not possible to delete them through the REST API.

0 commit comments

Comments
 (0)