Node.js File to Angular file upload - javascript

I'm trying to figure out how to connect a Node.js FileSystem to an angular file upload directive.
I get the file from Node using:
var file = brackets.test.FileSystem.getFileForPath('/Users/me/Desktop/new-year-2015.jpg')
The resultant object looks like:
File {_isFile: true, _name: "new-year-2015.jpg", _parentPath: "/Users/me/Desktop/", _path: "/Users/me/Desktop/new-year-2015.jpg", _fileSystem: FileSystem…}_fileSystem: FileSystem_id: 89_impl: (...)_isFile: true_name: "new-year-2015.jpg"_parentPath: "/Users/me/Desktop/"_path: "/Users/me/Desktop/new-year-2015.jpg"
I'm trying to upload this object to server with:
$upload.upload({
url: 'https://localhost/upload',
file: file,
fileName: getFilenameFromPath(file),
});
On server the file is received as null.
How can I use a node.js file object in an angular file upload?

Related

React Native get real path of file on Android

I'm using react-native-document-picker to select a file and react-native-fs to upload the file.
try {
const res = await DocumentPicker.pick({
type: [DocumentPicker.types.allFiles],
copyToCacheDirectory: false
});
I'm getting the uri as - content://com.android.providers.media.documents/document/image%3A24
Then I'm converting this to actual path using react-native-get-real-path.
RNGRP.getRealPathFromURI(res[0].uri)
.then(filePath =>{
realFilePath = filePath
console.log("real-path-->"+filePath)
ToastAndroid.show(filePath, ToastAndroid.SHORT);
})
The above method gives the path as /storage/emulated/0/Download/output-onlinepngtools.png but this working for few files, for others I'm getting null as the real-path.
This is the error I see while uploading the file - [Error: Socket is closed]

Failed to load resources status: 404 javascript with django

i am running javascript frontend with django backend.
Here is the structure
-app
--templates
---index.html
--static
---index.js
---haarcascade_frontalface_alt.xml
---style.css
--views.py
--urls.py
I can see css style in my index.html page. But the index.js file is unable to pick haarcascade_frontalface_alt.xml.
The code is as follow
const HAARCASCADE_URI = "haarcascade_frontalface_alt.xml"
this.classifier = new cv.CascadeClassifier();
let faceCascadeFile = "haarcascade_frontalface_alt.xml";
if (!this.classifier.load(faceCascadeFile)) {
await this.createFileFromUrl(faceCascadeFile, this.classifierPath);
this.classifier.load(faceCascadeFile)
}
but it say
Failed to load haarcascade_frontalface_alt.xml status: 404

How to load data from init-db.js in docker-compose

I am using docker to containerize a Flask API. The Flask API is accessing MongoDB using the "links" keyword in the docker-compose.yml file shown below:
app:
build: .
command: python -u app.py
ports:
- "5000:5000"
volumes:
- .:/app
#Linking db.
links:
- db
db:
image: mongo:latest
hostname: test_mongodb
environment:
- MONGO_INITDB_DATABASE=Exploit_Resources
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=pass
# Data provider.
volumes:
- ./init-db.js:/docker-entrypoint-initdb.d/init-db.js:ro
ports:
- 27017:27017
The link is working fine, and the file given under volumes (init-db.js) is supposed to provide the Mongo container with data. Here is my init-db.js file:
db = db.getSiblingDB("Exploit_Resources");
db.EDB.drop();
db.EDB.insertMany([
// Insert data here.
]);
The data I want to provide MongoDB comes from a remote csv file. I tried using PapaParse to access and feed the data, but I was having some difficulties importing it.
I tried this code inside the insertMany method:
const {StringStream} = require("scramjet");
const request = require("request");
request.get("https://srv.example.com/main.csv") // fetch csv
.pipe(new StringStream()) // pass to stream
.CSVParse() // parse into objects
.consume(object => console.log("Row:", object)) // do whatever you like with the objects
.then(() => console.log("all done"))
I got the code from here. It did not work because a ReferenceError was thrown, saying that 'require' is not defined.
Is there a way to fix this code or another way to get data from a remote csv file and provide it to the MongoDB container from the init-db.js?

Trouble with file upload using javascript swagger client and react

I have a problem with the source code generated by the Swagger codegen.
I want to upload a file with react. For this I have created a Dropzone and get a path to the file. If I use the generated client as it is in the documentation, it will not work. Unfortunately, the file is not sent. Only the file name. The debug console also does not show that binary data has been sent.
The request is not executed correctly. The file will not be uploaded. The parameter "file" is just the file name, instead of the binary data.
Swagger-codegen version
openapi-generator-cli-3.3.4.jar
Swagger declaration file content
Swagger .yaml:
/orders/upload:
post:
operationId: "orderUploadPart"
summary: "upload a textual or audio part of an order"
tags:
- "orders"
description: "This funktion uploads a textual or audio part of an order to the sense.ai.tion cloud system.
The result is the resource identifier, that must be used in the order request."
consumes:
- multipart/form-data
parameters:
- in: "formData"
name: "file"
type: "file"
required: true
description: "the file to upload"
- in: "formData"
name: "media"
type: "string"
enum:
- "text"
- "wav"
- "mp3"
required: true
description: "the media type of the the upload, can be ***text***, ***wav*** or ***mp3***"
Code:
var apiInstance = new SenseaitionApi.OrdersApi();
var file = "/path/to/file"; // File | the file to upload
var media = "media_example"; // String | the media type of the the upload, can be ***text***, ***wav*** or ***mp3***
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.orderUploadPart(file, media, callback);
It's like in: https://github.com/swagger-api/swagger-codegen/blob/master/samples/client/petstore/javascript/docs/PetApi.md#uploadFile
Screenshot Chrome DevTools
Command line used for generation
java -jar ${GOPATH}/bin/openapi-generator-cli.jar generate -i service_js_api.yaml -g javascript -o clients/javascript/senseaition-api-js -Dio.swagger.parser.util.RemoteUrl.trustAll=true
I found the mistake. The documentation of generated Javascript code is wrong. For uploading a file (Javascript object) must be passed, not the Path.
This line is wrong:
var file = "/path/to/file"; // File | the file to upload

ng-lb command in StrongLoop fails when using local-storage-connector

Dear Strongloop community
I am following the example provided by loopback for evaluating local-storage data store.
It works fine and I can use the rest API to create and retrieve files from the local file system (used as a data source).
I had to used "$ npm install loopback-component-storage --save".
the datasource defined in datasource.json is:
{
"db": {
"name": "db",
"connector": "memory"
},
"localFileSystemStorage": {
"name": "localFileSystemStorage",
"connector": "loopback-component-storage",
"provider": "filesystem",
"root": "./property_data"
}
}
the ./property_data directory is where all the data is stored. (relative to the project directory) and I can access it through rest with no issues.
Next I was trying to generate Angular service component so I can connect to the loopback server using Angular application. So inside the client directory I executed:
../example-2.0/client$ lb-ng ../server/server.js js/lb-services.js
and it failed:
/home/eval/projects/loopback-component-storage/example-2.0/node_modules/loopback-component-storage/lib/providers/filesystem/index.js:25
throw new Error('FileSystemProvider: Path does not exist: ' + this.root);
^
Error: FileSystemProvider: Path does not exist: ./server/storage
at new FileSystemProvider (/home/eval/projects/loopback-component-storage/example-2.0/node_modules/loopback-component-storage/lib/providers/filesystem/index.js:25:11)
at Object.module.exports.createClient (/home/eval/projects/loopback-component-storage/example-2.0/node_modules/loopback-component-storage/lib/providers/filesystem/index.js:17:10)
at Object.createClient (/home/eval/projects/loopback-component-storage/example-2.0/node_modules/loopback-component-storage/lib/factory.js:86:18)
at new StorageService (/home/eval/projects/loopback-component-storage/example-2.0/node_modules/loopback-component-storage/lib/storage-service.js:29:25)
at Object.exports.initialize (/home/eval/projects/loopback-component-storage/example-2.0/node_modules/loopback-component-storage/lib/storage-connector.js:12:19)
at DataSource.setup (/home/eval/projects/loopback-component-storage/example-2.0/node_modules/loopback-datasource-juggler/lib/datasource.js:332:17)
at new DataSource (/home/eval/projects/loopback-component-storage/example-2.0/node_modules/loopback-datasource-juggler/lib/datasource.js:109:8)
at Registry.createDataSource (/home/eval/projects/loopback-component-storage/example-2.0/node_modules/loopback/lib/registry.js:349:12)
at dataSourcesFromConfig (/home/eval/projects/loopback-component-storage/example-2.0/node_modules/loopback/lib/application.js:415:19)
at EventEmitter.app.dataSource (/home/eval/projects/loopback-component-storage/example-2.0/node_modules/loopback/lib/application.js:222:12)
Am I missing something?
I could really use some help here.
Thanks
In datasource.json file you have provided the relative path ./property_data with respect to /server folder.
Thus you need to call any method from that folder only.
Now when you are running it through the server then you are running it from /server folder thats why it runs without an issue but for angular Sdk generation you are doing it from client/ folder. Thus its genetating Path does not exist error.
Thus the correct syntax would be:
../example-2.0/server$ lb-ng server.js ../client/js/lb-services.js

Categories

Resources