Image uploaded on dropbox does not have any content - file-upload

I am using Dropbox api uploadAsync to upload an image which in itself is passed in raw data form from another POST request. The upload is successful in terms that there is a file now on dropbox. But when I access that file, it says it has no contents. Why is it uploading file without content? Here is what I have tried
MemoryStream memoryStream = new MemoryStream();
imageStream.Position = 0;
memoryStream.Position = 0;
imageStream.CopyTo(memoryStream);
var task = Task.Factory.StartNew(() => {
var dropboxClient = new DropboxClient(token);
var folder = "XYZ";
var fileName = "ABC";
var upload = dropboxClient.Files.UploadAsync(folder + "/" + fileName, Dropbox.Api.Files.WriteMode.Add.Instance, autorename:true, body: memoryStream);
upload.Wait();
});
task.Wait();
imageStream is the Stream which I get from a POST request. It has raw image data.
Any help would be appreciated

Related

Upload an image to Strapi using Xamarin.Forms

Currently im working on a xamarin forms app, that upload image to Strapi API. To take a picture from the camera i'm using CrossMedia Plugin
var photo = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions() { PhotoSize= PhotoSize.Small, CompressionQuality = 100 });
if (photo != null)
ProductPic.Source = ImageSource.FromStream(() => { return photo.GetStream(); });
than send the photo in post Method :
HttpClient httpClient = new HttpClient();
MultipartFormDataContent mt = new MultipartFormDataContent();
photo.GetStream().Position = 0;
StreamContent imagePart = new StreamContent(photo.GetStream());
imagePart.Headers.Add("files", "jpg");
mt.Add(imagePart, string.Format("image"), string.Format("bsk.jpeg"));
var response = await httpClient.PostAsync("http://111.111.111.111:2222/upload", mt);
The problem that im facing this error
"{\"statusCode\":400,\"error\":\"Bad Request\",\"message\":\"Bad Request\",\"data\":{\"errors\":[{\"id\":\"Upload.status.empty\",\"message\":\"Files are empty\"}]}}
The error points the image part is empty , try to replace StreamContent with ByteArrayContent , send ByteArray instead of Stream .
HttpClient httpClient = new HttpClient();
MultipartFormDataContent mt = new MultipartFormDataContent();
mt.Headers.ContentType.MediaType = "multipart/form-data";
var upfilebytes = File.ReadAllBytes(photo.Path);
mt.Add(new ByteArrayContent(upfilebytes, 0, upfilebytes.Count()), string.Format("image"), string.Format("bsk.jpeg"));
var response = await httpClient.PostAsync("http://111.111.111.111:2222/upload", mt);
Refer to
https://stackoverflow.com/a/61095848/8187800.

how to upload a file using autoit in protractor

I tried the code below but it isn't working. Can someone help me understand how to upload a file using autoit in protractor?
var path = require('path');
var file = "../Snaptrude/plans/"+filepath+"";
console.log('file path',file)
var filePath = path.resolve(__dirname, file);
// browser.sleep(3000);
element(by.css('input[type="file"]')).sendKeys(filePath);
var path = require('path');
var appRoot = require('app-root-path');
var uploadFile = appRoot + '/path of the file which you want to upload';
var deferred = protractor.promise.defer();
var control = element(by.xpath('you need to take the xpath of uploaded file'));
control.getText().then(function(text) {
if (text == 'filename') {
enter code hereconsole.log("success");
}
without using autoit you can upload the file using above way, if you are work on any web application.

Titanium - Get image file from filesystem on Android

I have a problem getting an image from filesystem. On iOS works fine.
First of all, I save a remote image in the filesystem with this function:
img.imagen = url from the remote image (e.g. http://onesite.es/img2.jpeg)
function descargarImagen(img, callback){
var path = img.imagen;
var filename = path.split("/").pop();
var xhr = Titanium.Network.createHTTPClient({
onload: function() {
// first, grab a "handle" to the file where you'll store the downloaded data
var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, filename);
f.write(this.responseData); // write to the file
Ti.API.debug("-- Imagen guardada: " + f.nativePath);
callback({path: f.nativePath});
},
timeout: 10000
});
xhr.open('GET', path);
xhr.send();
}
Now, I want to share this image creating an Android Intent:
args.image = f.nativePath(in the previous function)
var intent = null;
var intentType = null;
intent = Ti.Android.createIntent({
action: Ti.Android.ACTION_SEND
});
// add text status
if (args.status){
intent.putExtra(Ti.Android.EXTRA_TEXT, args.status);
}
// change type according to the content
if (args.image){
intent.type = "image/*";
intent.putExtraUri(Ti.Android.EXTRA_STREAM, args.image);
}else{
intent.type = "text/plain";
intent.addCategory(Ti.Android.CATEGORY_DEFAULT);
}
// launch intent
Ti.Android.currentActivity.startActivity(Ti.Android.createIntentChooser(intent, args.androidDialogTitle));
What I'm doing wrong?

How to send file using XMLHttpRequest?

How to send file in QML. I tried something like:
var pathImage = "file:assets-library:/asset/asset.JPG%3Fid=0EEAFD08-5878-45D1-9045-025FD5CAB907&ext=JPG"
var request = new XMLHttpRequest();
var request_url = API_URL + '?method=postImage';
request.open('POST', request_url);
var params = "image=" + pathImage;
request.send(params);
But it doesn't send file.
How to send file in QML without C-code?

System.Text.Encoding in WinJS

Is there an equivalent of
System.Text.Encoding.UTF8.GetString(fileContent)
in WinJS (Windows 8 Store App written in javascript/HTML)?
EDIT. fileContent is a byte array.
There is no strict equivalent of System.Text.Encoding.UTF8.GetString in WinJS, but you can try implement file reading to string as follows:
file.openReadAsync().done(function (stream) {
var blob = MSApp.createBlobFromRandomAccessStream(file.contentType, stream);
var reader = new FileReader();
reader.onload = function(event) {
var fileAsText = event.target.result;
};
reader.readAsText(blob, 'UTF-8');
});
In most cases (file upload via XHR, displaying file) you don't need to have file contents as text, so just use Blob then.
CryptographicBuffer.convertBinaryToString can be used for this.
var crypt = Windows.Security.Cryptography;
var bytes; // = new Uint8Array(100);
// TODO - set bytes variable with uint8array
var buffer = crypt.CryptographicBuffer.createFromByteArray(bytes);
var text = crypt.CryptographicBuffer.convertBinaryToString(
crypt.BinaryStringEncoding.utf8, buffer);