I am creating a web application using Laravel 5.
I want to use AngularJS.
I have created a file named all-products.php in views directory which contains data in JSON format.
When I gave path to this file in http.open() it gives an error as http://localhost/user/app/resources/views/all-products.php 404 (Not Found).
Here is my JS code:
var app = angular.module("products", []);
app.controller("productsController", function($scope, $http){
$http.get("http://localhost/user/app/resources/views/all-products.php").success(function(response) {
$scope.records = response.records;
});
});
Please tell me how I can solve this ?
Thanks in advance.
You need to put the file in your public directory e.g.
http.get("http://localhost/all-products.json")
Would be a file at path public\all-product.json (assuming your homepage is http://localhost).
Or, if the file is dynamically created, you need to create a route, and return the .json data from there using Response::json(['some data]);
Related
I post the data in API and want to edit this data after getting from API. when I try to edit the data it gives me the following error:
CKEditorError: datacontroller-set-non-existent-root: Attempting to set data on a non-existing root. Read more: https://ckeditor.com/docs/ckeditor5/latest/framework/guides/support/error-codes.html#error-datacontroller-set-non-existent-root
<CKEditor
editor={ClassicEditor}
onChange={this.handleChange}
data={html}
></CKEditor>
Here is the dummy solution for now, I don't know what is the proper reason of that:
Create one variable:
let a = "";
Replace the content of the variable a with the your content coming from the API
and parse it with htmlparser:
let data = a.replace("", htmlparser(/*data coming from your api*/))
How do you append to the file instead of overwriting it?
The file-system module documentation explains how to write to a file:
The following example writes some text to a file. It will create a new
file or overwrite an existing file.
var documents = fs.knownFolders.documents();
var file = documents.getFile("Test_Write.txt");
// Writing text to the file.
file.writeText("Something")
.then(function () {
// Succeeded writing to the file.
}, function (error) {
// Failed to write to the file.
});
My end goal is to write log files to disk as the nativescript app runs. It'd be too inefficient to read the contents of the log file into memory, append the new log message, and then write it all back to the file.
UPDATE:
Ended up logging to an sqlite database. This has some benefits and drawbacks, but for other's trying to solve a similar issue it's another direction to try.
NativeScript currently doesn't provide a appendFile function for its file system module.
Reference: https://github.com/NativeScript/NativeScript/issues/4462
A sample implementation of appendFile (text only) can be found here: https://github.com/EddyVerbruggen/nativescript-node/blob/dc93ab48a84c410bc8d6f46527fb171799f8bfeb/fs.js#L233-L254
It is using the "read and then write" method as you said.
Firstly, I Want to read excel file which user is uploading and saving data to DB.
I have tried load function using excel API.
Excel::load($file_name_real, function($input) {
$results = $input->all();
// $input->dump();
})->download('xls');
But its not happening So now I want to move File to server then will read file then will unlink. By following function
$input1->move('/laravel');
But its not moving file.
I'm trying to allow users to upload photos to the server and then view them (all users can view all photos) in production (NOT development). While in development mode everything is simple - I can upload the files to the public folder and then read then from there, in production mode I don't have access to the public folder anymore (as this approach is for static accesses and not dynamic).
So, I have 2 issues:
Upload: currently I can't understand how to save the uploaded photos to a specific folder, without using an absolute path to the location where I want the photos to be saved.
Here is the code for upload (similarly to this guide - https://www.playframework.com/documentation/2.4.x/ScalaFileUpload):
def uploadPhoto = Action(parse.multipartFormData) { request =>
import play.api.mvc.MultipartFormData
import play.api.libs.Files.TemporaryFile
import java.io.File
import java.nio.file.Path
import java.nio.file.Paths
try {
val multipartForm: MultipartFormData[TemporaryFile] = request.body
val pathDev: Path = Paths.get("./public/img");
val pathProduction: Path = Paths.get("/...<[full path]>.../public/img");
val imgPath =
if (Files.exists(pathDev)) { pathDev.toString() }
else { pathProduction.toString() }
val newFile = img.get.ref.moveTo(new File(s"$imgPath/$imgName"))
// [HERE I save the path to my DB]
Ok(Json.obj("imgName" -> imgName))
} catch {
case e: Exception =>
BadRequest("unknown error")
}
}
It is unclear to me how to serve the uploaded images back to users that want to see them.
I want to dynamically change the scr in the img html tag to show the relevant image, like so: $("#img").attr("src",'assets/img/1.jpg');
But as the public folder is not available, the images are "not there" (at least until I will "stage" the project and re-run it - https://www.playframework.com/documentation/2.4.x/Assets).
I tried the following approach (https://www.playframework.com/documentation/2.4.x/ScalaStream):
I have added the following line to my conf/routes file:
GET /img/:filename controllers.MyController.getPhoto(filename)
and defined the following function in the controller:
def getPhoto(filename: String) = Action {
Ok.sendFile(new java.io.File("./img/" + filename))
}
But the browser is downloading the file instead of showing it...
These are related:
Handling dynamic created files in play 2
How to serve uploaded files in Play!2 using Scala?
Any assistance will be very appropriated.
Here's how I fix this
ISSUE 1
For upload file path, in play you can define configurations in conf/application.conf file, and you can use different file for production mode, using -Dconfig.file=/path/to/the/file.
So I defined an attribuite called myapp.image.base, in debug mode just set it to "", and in production mode (I created a file called conf/application.prod.conf) , I put an absolute path to it.
So in my code, I always use the following command for file path (it's in Java, but you should find a similar way in Scala for reading configuration)
Play.application().configuration().getString("myapp.image.base")+"img"
ISSUE 2
For serving image
You need to create a router.
First in your routes file, add something like this:
GET /user/images/:name controllers.Application.imageAt(name:String)
And write a simple file reader in action imageAt which return the file in stream. Again my sample is in Java but you should archive the same using Scala
File imageFile = new File(ReportFileHelper.getImagePath());
if (imageFile.exists()) {
//resource type such as image+png, image+jpg
String resourceType = "image+"+imageName.substring(imageName.length()-3);
return ok(new FileInputStream(imageFile)).as(resourceType);
} else {
return notFound(imageFile.getAbsoluteFile());
}
After that, the images is reachable from url /user/images/
Play reads files off the classpath (which includes the assets directory). On the JVM, the classpath is immutable—once started, files added to folders on the classpath will not actually be added to the classpath.
This works in development mode because Play reloads the classpath whenever it detects a change. That same hot-reloading is not enabled in production (for good reason).
Hello everyone I would like to ask How can I download .jpg file from web to my project's folder which I have created "uploads" ?
I'm trying to downlaod youtube thumbnail image to my" uploads" folder.
My controller:
var fileName = Path.GetFileName(http://img.youtube.com/vi/RUgd_GDPhYk/1.jpg);
var path = Path.Combine(Server.MapPath("~/uploads/"), fileName);
file.SaveAs(path);
Take a look at System.Net.WebClient, a .NET class which allows you to make requests for resources via HTTP.
http://msdn.microsoft.com/en-us/library/system.net.webclient(v=vs.100).aspx
Checked example provided.
var client = new System.Net.WebClient();
var uri = "http://img.youtube.com/vi/RUgd_GDPhYk/1.jpg";
// Here, we're just using the same filename as the resource we're after.
// You may wish to change this to include extra stuff because you'll
// inevitably run into a naming clash - especially with stuff like 1.jpg
var targetFilename = Path.GetFileName(uri);
client.DownloadFile(uri,
Path.Combine(Server.MapPath("~/uploads"), targetFilename));