How can I send image to API which is already exists on server? - api

I need to send image to API which is already uploaded to server. How can I do that?
Application is developed on Laravel Framework 5.7. For API I'm using GuzzleHTTP Client package.
Thank you in advance.

You could try to encode your file to Base64, then send it back from your API.
Assume this is your image:
$image_path = storage_path($model->image);
$image_data = \File::get($image_path);
Returning Image Encoded on Base64:
return response()->json(['image' => base64_encode($image_data)]);
Others way to make your response (with the file):
https://laravel.com/docs/5.8/responses#json-responses
https://laravel.com/docs/5.8/responses#file-downloads

Related

How to send files created in a mobile app without overwritten the name in the server when the app is used by different users

I am working in a mobile app. In this mobile app I need to send a file (.wav file) to a server.
At this moment, I am working locally and testing my app. I am creating an audio file in the mobile app with a defined name and extension. However, I'm wondering, If my app is used by more people,
can it happen that, if the different users use the app and send the file at the same time, as the file name is already defined in the app, in the server this file is going to be overwitten ?
What is usually the behaviour in a situation like this? And if the file is overwitten, how can I overcome this problem, since diffrent users may send a different file?
Details:
API : FAST API :
async def something(file: UploadFile = File(...)):
with open(f"{file.filename}", "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
#saved teh file
developing with React NAtive
Using Expo AV to do the audio
sending the audio using fetch
sending method:
formData.append("file",
uri: path,
name: "audio.wav",
type: "audio/wav",
....
method: "POST",
headers: {
"Content-Type": "multipart/form-data",
},
body: formData,
Thanks !
This scenario should be handled completely by the server. The client should not contain any logic on what files are already located on the server and which are not.
Let the server create a new uuid for each new file that it receives.
Thus, the metadata for a file upload could be the following object.
{
uuid: "11edc52b-2918-4d71-9058-f7285e29d894",
fileUri: "",
}
Thus, the server needs to be adapted to solve this problem. The file is then stored along with its unique identifier. No name clash should occur.
Of course, if you want to display the files in the client later, then you need some name property, which could be the fileUri appended with a counter variable if the same fileUri exists multiple times. This should be handled by the server as well.

Nest.js and Archiver - pipe stream zip file into http response

In my node.js application I'm downloading multiple user files from AWS S3, compress them to single zip (with usage of Archiver npm library) file and send back to client. All the way I'm operating on streams, and yet I can't send files to client (so client would start download after successful http request).
const filesStreams = await this.awsService.downloadFiles(
document?.documentFiles,
);
const zipStream = await this.compressService.compressFiles(filesStreams);
// ts-ignore
response.setHeader('Content-Type', 'application/octet-stream');
response.setHeader(
'Content-Disposition',
'attachment; filename="files.zip"',
);
zipStream.pipe(response);
Where response is express response object. zipStream is created with usage of Archiver:
public async compressFiles(files: DownloadedFileType[]): Promise<Archiver> {
const zip = archiver('zip');
for (const { stream, name } of files) {
zip.append((stream as unknown) as Readable, {
name,
});
}
return zip;
And I know it is correct - because when I pipe it into WriteStream to some file in my file system it works correctly (I'm able to unzip written file and it has correct content). Probably I could temporarily write file in file system, send it back to client with usage of response.download and remove save file afterwards, but it looks like very inefficient solution. Any help will be greatly appreciated.
So I found a source of a problem - I'll post it here just for record, in case if anyone would have same problem. Source of the problem was something totally different - I was trying to initiate a download by AJAX request, which of course won't work. I changed my frontend code and instead AJAX I used HTML anchor element with src attribute set on exactly same endpoint and it worked just fine.
I had a different problem, but rather it came from the lint. I needed to read all files from the directory, and then send them to the client in one zip. Maybe someone finds it useful.
There were 2 issues:
I mismatched cwd with root; see glob doc
Because I used PassThrough as a proxy object between Archiever and an output, lint shows stream.pipe(response) and type issue... what is a mistake - it works fine.

MobileFirst: Handling Binary Responses in React Native

I'm new to MobileFirst development. I need to download a zip file from one of the adapters. I am able to do this when invoking REST API call eg using Postman. But I'm not sure how to handle this when using MFS sdk:
Here's my code to access the adapter:
var resourceRequest = new WLResourceRequest(url,
WLResourceRequest.GET
);
const resp = await resourceRequest.send()
I log the response and it's showing special characters in the responseText.
Is there a way to handle the binary response?
Thank you in advance for your help!
For any binary content, you will have to convert the content to Base 64 in your adapter and revert it back to binary in your app.
This is because the WLResourceRequest APIs are designed for handling text based data.
If your binary content is rather large, then it is best to host the file somewhere and return the link to the file from your adapter.

Windows Phone: Upload Progress

I've built a Windows Phone app that needs to upload an image to a server. I'd like to display an upload progress percentage instead of the generic progress bar.
I'm uploading the images as Byte[] to my server using a WCF service (basitHttpBinding).
Has anyone managed to do this yet?
On WebRequest or WebClient this would be easier but since you're using WCF guess we have to take another approach!
Please check if this approach will work for you!
Note that this approach uses a Stream when sending/receiving, that shouldn't be an issue given that internally it will be converted to a byte array (like you have).

Uploadify with NodeJS | Post parameters not being sent

I'm building my latest app using nodeJS and need to be able to upload multiple files. I have chosen to use Uploadify flash based file uploader.
Unfortunately the 'scriptData' vars don't seem to be being sent. Usual method of retrieval of POST vars in node looks like var postDataValue = req.body.postDataKey; but with Uploadify the req.body object is empty.
Hope someone can help with this.
I had to send the parameters via query string in the end. Must be something to do with NodeJS.
It's not Node.js. req.body is not part of node. It's built into BodyParser and provided by Connect. Mainly used in Express.
See http://senchalabs.github.com/connect/middleware-bodyParser.html
If you don't use it, the req.body object should be empty.