I am using ngf-file-upload module to upload file through angularjs and laravel.
This is my html initiatior :
<div class="button" ngf-select="profile.uploadFiles($file, $invalidFiles)">Upload on file select</div>
This is my controller in angular
vm.uploadFiles = function(file) {
if (file) {
file.upload = Upload.upload({
url: 'http://localhost/api/uploadImg',
data: {file: file}
});
}
}
This is my upload API code in Laravel.
public function uploadImg()
{
$data = Input::file('file');
return $data;
}
My angular app is running on 9000 port and laravel in default port.
The request from angular is hitting the api built in laravel but the thing is that it's missing the file. Although my request header looks suspicious in Browser's Network. Like content-type is not multipart/form-data
This is the link of danial farid js fiddle
I am using the same code, with alterations to sync with my api, that is mentioned in the fiddle.
The strange is thing that when i am giving my local API path to this fiddle then it is working fine. But it's not working on my project locally. I think there is something that i am missing on my angular side.
This is the image of param that is send when i am trying to upload file from local project
This is the image of param that is send when i am trying to upload file from fiddle to my api
My Conclusion
There is no problem in the code because I am able to upload the file from a html file( not in my angular project) that is specifically i have created to test file uploading and it's running.
BUT
It's failing when i am trying to upload through my project.
I have attached the images of my console one case is working one(uploading image through random file) and other is not working case (where i am uploading file through my project).
I don't what to do next. I think there is something in my angular project that is creating a obstacle in my uploading process.
Interceptors
Yes, they were creating problem in file upload because we have set headers in interceptors
and the default headers we have set in interceptors
config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
For every request that has been made from our angular app is going through the interceptor and the interceptor was modifying every request with these headers. So i Have to remove this header from the interceptor.
Note
Don't forget to add the removed header in every other request
Related
Uploading files using antd Dragger component, everything works fine but when I upload the files, I get Proxy status in response instead of file? What does proxy here represent?
I am using this example https://ant.design/components/upload/#components-upload-demo-drag. Why am I not getting file in response directly?
Based on #29614
Upload should always return new file proxy.
If you need to object install for example antd#4.9.1. Then it returns object to you.
I have an Angular 1.x application using the popular ng-file-upload to make a request to the Rackspace OpenCloud library for uploading files to a CDN.
In a nutshell the script below - uploads a file, sends it to the backend of the application & in turn sends a request to the Rackspace Container and returns the public URL. This all works fine in Postman without issues - when I implement this into my Angular application I have some issues with CORS (see upload code below) - the frontend Angular sends a request to our backend app which in turn sends a request to the OpenCloud to upload a file to the Rackspace CDN and returns the public URL.
// frontend Angular app
Upload.upload({
url : 'https://asite.com/users/request',
data : {
__token : $localStorage.token.value,
fileToUpload : file
}
// additional code below not shown for clarity
In the console log I see the following : ( I have the changed the actual url below for security purposes) :-
Failed to load https://xxxxxxxxxxxxxx-2426685a61633353dfd5b28cdbf2b449.ssl.cf3.rackcdn.com/5a7d6de352a949.48129019.pdf: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://domain.local' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
How do I prevent this CORS error so it loads the file from the CDN?
fixed - the issue was actually on the PHP backend (in case it helps out somebody else)
$object = $container->DataObject();
$object->Create(array(
'name' => $filename,
'content_type' => $mime,
'extra_headers' => array('Access-Control-Allow-Origin' => '*'
)), $file);
I needed to add this in the extra headers array when creating the file using the OpenCloud SDK
Seems so simple from examples I see on the web but when I try to load a local json file from my Angular 2 application inside my service.ts, I get a 403 forbidden error. My application runs inside a Servicestack selfhosted service. Works fine when I do a request like http://localhost:8087/customers/ but the same data inside a local json file in my app directory will not (http://localhost:8087/app/customers.json)
What do I have to do to allow my application to access that file?
this._http.get('app/customers.json').toPromise()
.then(function (response) {
console.log(JSON.stringify(response))
}).catch(this.handleError);
Suggestion from #mythz fixed my issue.
I added this line in my appHost file.
HostConfig.Instance.AllowFileExtensions.Add("json");
In my serverless app, I need to generate pdf dynamically and then upload that generated pdf into aws-s3 bucket. But in serverless, we can only sent json request, so I sent my html string to serverless, it generate pdf and then save that generated pdf into local machine. I think, that part I can do, But my problem is, I need to upload that newly generated pdf into aws-s3. My code is given below:
Angular Code:
$scope.generate_pdf = function() {
var html = angular.element('html').html();
var service = API.getService(); // sent html string as json for generating pdf
service.downloadPdf({}, { html : html },
function(res) {
console.log("res : ", res); // res.url: /tmp/dynamica-generate.pdf
// pdf is saved into '/tmp' folder
// now I want to upload pdf from '/tmp' folder to aws-s3
}, function(err) {
console.log("err : ", err);
});
};
How can I solve this problem, Thanks in Advance.
If you want to upload file from your local machine to S3 bucket. Install AWS Cli on your local machine. after that is done add a cron job for this command
aws s3 sync /tmp/* s3://mybucket
You should be able to use the aws Javascript S3 sdk to integrate JS code into your angularJS code.
You can follow from this blog post and github code
Its also mentioning about setting up the credentials using a public IAM account. Depending how you serve the files to your clients, you might check also using Pre-Signed URL’s.
Using ng-file-upload.js to upload files. Two questions: To which location the files get uploaded by default and how to change this location?
From github documentation (https://github.com/danialfarid/ng-file-upload)
var upload = Upload.upload({
url: 'server/upload/url', // upload.php script, node.js route, or servlet url
...
Alternative way of uploading, send the file binary with the file's content-type.
Could be used to upload files to CouchDB, imgur, etc... html5 FileReader is needed.
It could also be used to enable progress for regualr angular $http() post/put requests.
var uploadHttp = Upload.http({
url: '/server/upload/url',
...