Skip to content

an example for saving image with ParseFile #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
alparslantopbas opened this issue May 21, 2019 · 4 comments
Closed

an example for saving image with ParseFile #175

alparslantopbas opened this issue May 21, 2019 · 4 comments

Comments

@alparslantopbas
Copy link

could you please, can you give a basic working example an image save/upload with ParseFile to Parse Server?

@RodrigoSMarques
Copy link
Contributor

Hi @alparslantopbas,
you have to have a File. In this example, I downloaded an internet file, saved it to Local Storage, and am loading it into Parse

  var file = await _downloadFile(
      "https://i2.wp.com/blog.openshift.com/wp-content/uploads/parse-server-logo-1.png",
      "image.png");
  print("saveFile");
  ParseFile parseFile = ParseFile(file, name: "image.png", debug: true);
  var fileResponse = await parseFile.save();
  if (fileResponse.success) {
    parseFile = fileResponse.result as ParseFile;
    print(parseFile.toString());
    print("Upload with success");
  } else {
    print("Upload with failed");
  }

@alparslantopbas
Copy link
Author

Thanks for the quick response, but I think this code is for file download. How can I upload a file to ParseServer ?
This is my code, but it doesn't work:
ParseFile imageOnYuz = ParseFile(_imageFileOnYuz, debug: true);

imageOnYuz.name = randomAlphaNumeric(10) + '.png';
imageOnYuz.url = '$APIURL/files/$APPKEY/${imageOnYuz.name}';
imageOnYuz.file = _imageFileOnYuz;
imageOnYuz.className = tblImages;

ParseObject images = new ParseObject(tblImages);
images.set('ImageName', imageOnYuz);
images.set('OnArkaYuz', 'OY');

var imagesResponse = await images.save();
if (imagesResponse.success) {
print('Upload with success');
} else {
print('Something went wrong');
}

@RodrigoSMarques
Copy link
Contributor

RodrigoSMarques commented May 25, 2019

Hi @alparslantopbas ,

This code is not for download from the archive. The file download was just to create a file in the local store.

The file can be an image selected through the image_picker plugin.

The ParseFile constructor needs the File and the file name. The Debug parameter is optional.

After saving ParseFile you can save it to a ParseObject.

See the example below

  File image = await ImagePicker.pickImage(source: ImageSource.gallery);
  ParseFile parseFile = ParseFile(image, name: "image.jpg", debug: true);
  var fileResponse = await parseFile.save();
  if (fileResponse.success) {
    parseFile = fileResponse.result as ParseFile;
    print(parseFile.toString());
    print("Upload with success");
  } else {
    print("Upload with failed");
    return;
  }

  var parseObject = ParseObject("TestObjectFile", debug: true);
  parseObject.set<ParseFile>("fileImage", parseFile);
  var apiResponse = await parseObject.save();

  if (apiResponse.success) {
    print('ok');
  } else {
    print("Error: " + apiResponse.error.toString());
  }

@alparslantopbas
Copy link
Author

Hi @RodrigoSMarques ,

Thank you very much, it works 👍
I finally get it and fix my code as you wrote :-)

@alparslantopbas alparslantopbas changed the title example for image save with ParseFile example for saving image with ParseFile May 25, 2019
@alparslantopbas alparslantopbas changed the title example for saving image with ParseFile an example for saving image with ParseFile May 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants