laravel 5.3 image upload file path store in variable - object

Symfony\Component\HttpFoundation\File\File Object ( [pathName:SplFileInfo:private] => employee\logo_5781432114142.jpg [fileName:SplFileInfo:private] => logo_5781432114142.jpg )
I want to store path-Name in a variable How would I store the file path

tried an alternate way to store image file path
example:- $imageName = time().'.'.$request->image_file->getClientOriginalExtension();

Related

Django. TemporaryUploadedFile

I upload a file through the form, check it, and only after checking it I want to add it to my database.
form = BookForm(request.POST, request.FILES)
file = form.files
path = file.get('book_file').temporary_file_path()
in path - '/tmp/tmpbp4klqtw.upload.pdf'
But as soon as I want to transfer this file from the temporary storage to some other folder, I get the following error:
path = os.replace(path, settings.MEDIA_ROOT)
IsADirectoryError: [Errno 21] Is a directory: '/tmp/tmpbp4klqtw.upload.pdf' -> '/home/oem/bla/bla'
Can't understand why this file is not in reality? What can I do about it? Is it possible to set some special path for the "temporary file"?
UPD:
You should use path = os.replace(path, settings.MEDIA_ROOT + '/name-of-file.pdf') – Willem Van Onsem
os.replace(…) [python-doc] expects a filename as target if you specify a file as source, so you can move this to:
os.replace(path, f'{settings.MEDIA_ROOT}/name-of-file.pdf')
you can also make use of shutil.move(…) [python-doc] to specify the directory, this function will also return the filepath of the target file:
from shutil import move
target_file = move(path, settings.MEDIA_ROOT)

Resize image and store into Storage in Laravel 7

So far I'm uploading an image with the following code bellow:
if($request->hasFile('image')) {
$path = $request->image->getClientOriginalName();
$name = time() . '-' . $path;
$news->image = $request->file('image')->storeAs('public/news', $name);
}
It checks for file image, revert to it original name, creating a time format attached to the image filename and uploading into storage in the desired folder.
How can I resize that image before uploading into the file directory?
I've read about the Intervention, which is included in Laravel 7 but can someone help me to achieve this using Intervention and combine with my logic of uploading an image?
I've tried like this:
use Intervention\Image\ImageManagerStatic as Image; // use Intervention
if($request->hasFile('image')) {
$path = $request->image->getClientOriginalName();
$resize = Image::make($path)->fit(300);
$name = time() . '-' . $resize;
$news->image = $request->file('image')->storeAs('public/news', $name);
}
but I'm getting Image source not readable error.
Visibly, Intervention can't read your image, check the path you give it.
dd($path);
In my case, this is how I proceed:
$img = Image::make('storage/'.$banner)->resize(800, 250);
Then to resize your image before uploading it, you can do like this:
//$image is the temporary path of your image (/tmp/something)
$image = $request->image;
//Create a Image object with the tmp path
$resized_img = Image::make($image);
//Do what you want and save your modified image on the same temporary path as the original image.
$resized_img->fit(300)->save($image);
//Upload your image on your bucket and get the final path of your image
$path = $image->storeAs('public/news', $name)
I've found a solution after a long testing. This is my code bellow:
if($request->hasFile('image')) {
$image = $request->file('image');
$imageName = $image->getClientOriginalName();
$fileName = 'public/news/' . time() . '-' . $imageName;
Image::make($image)->resize(600,300)->save(storage_path('app/' . $fileName));
$news->image = $fileName;
}
The main issue was the path and also I don't need to use storeAs() function, simply just using the Intervention functions...that's all.
This approach is lot more flexible and it's very easy to implement additional features from the wonderful Intervention library.

Rails 5.2 Rest API + Active Storage - Upload file blob received from an external service

We are receiving a POST call from an external service, which contains the file blob (in Base64 encoding), and some other parameters.
# POST call to /document/:id/document_data
param = {
file: <base64 encoded file blob>
}
We would want to process the file and upload it to the following model
# MODELS
# document.rb
class Document < ApplicationRecord
has_one_attached :file
end
In the Controller method handling the POST call
# documents_controller.rb - this method handles POST calls on /document/:id/document_data
def document_data
# Process the file, decode the base64 encoded file
#decoded_file = Base64.decode64(params["file"])
#filename = "document_data.pdf" # this will be used to create a tmpfile and also, while setting the filename to attachment
#tmp_file = Tempfile.new(#filename) # When a Tempfile object is garbage collected, or when the Ruby interpreter exits, its associated temporary file is automatically deleted.
#tmp_file.binmode # This helps writing the file in binary mode.
#tmp_file.write #decoded_file
#tmp_file.rewind()
# We create a new model instance
#document = Document.new
#document.file.attach(io: #tmp_file, filename: #filename) # attach the created in-memory file, using the filename defined above
#document.save
#tmp_file.unlink # deletes the temp file
end
Hope this helps.
More about Tempfile can be found here.

Golang excel file reading

I'm using tealeg xlsx library to read an excel file https://github.com/tealeg/xlsx . They have documentation here https://godoc.org/github.com/tealeg/ . It works perfectly fine if I call the OpenFile() by local directory, but I wanted to use an http.Request.FormFile() return object which is of type multipart.Form. How do I use this file to be read by the tealeg package?
Tealeg's OpenReaderAt() looks like something I should use, but the multipart. Form object returned from http.Request.FormFile() returns a file interface but I'm not sure how to access the readerAt object? https://golang.org/pkg/mime/multipart/#File
func OpenReaderAt(r io.ReaderAt, size int64) (*File, error)
xlsx.OpenReaderAt takes in an io.ReaderAt interface and multipart.File also implements io.ReaderAt.
So you can directly pass it to xlsx.OpenReaderAt
var (
file multipart.File
size int64
err error
)
file, _,err = req.FormFile("key")
// size = // Calculate size
xlsx.OpenReaderAt(file,size)

Download xls file from application rails 4

I am using rails 4 application, where i have a xls file in my applications public folder ,and am having a link in view page after clicking that link that xls file should download to system.no need to write anything in that file its just a default template.
I use:
view:
<%= link_to "Dowload Template", admin_job_templates_path, :file_name => 'Job_Upload_Template.xlsx' %>
controller :
def index
require 'open-uri'
file_path = "#{Rails.root}/public/Job_Upload_Template.xlsx"
send_file file_path, :filename => "Job_Upload_Template.xlsx", :disposition => 'attachment'
end
also tried send_data method
def index
require 'open-uri'
url = "#{Rails.root}/public/Job_Upload_Template.xlsx"
data = open(url).read
send_data data, :filename =>'Job Template'
end
Am getting the below error.
No such file or directory # rb_sysopen -
/home/amp/workspace/LA_Tracker/public/Job_Upload_Template.xlsx
am not able to dowload the file, Plese help.
It's telling you that file doesn't exist. Do you have the capitalization correct? Double check. Also, what happens if you type:
ls /home/amp/workspace/LA_Tracker/public/Job_Upload_Template.xlsx
into the command line? Can your OS find the file?

Resources