Upload file to public folder of vuejs - function

would like to know if there is a way to upload files at public folder using vuejs coding
I do have a code but it is build to move the file at laravel public folder. do vuejs have that kind of function?
here are what i have so far on my function. hope you guy's can help me
form code
<!-- Form Upload -->
<div class="row">
<div class="col-sm-6 offset-3">
<div class="form-group">
<label for="exampleFormControlFile1">Upload</label>
<input
type="file"
ref="file"
class="form-control-file"
#change="onFileChange"
>
<small class="text-muted">
for slideshow images
<br />
size: 1280 x 630 pixel for good quality
</small>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button
type="button"
class="btn btn-primary"
data-dismiss="modal"
#click="uploadFile"
:disabled="disableBtn"
>Upload</button>
</div>
method code
onFileChange(e) {
this.file = this.$refs.file.files[0];
},
uploadFile() {
if (this.file == "") {
this.alertClass = "alert alert-danger";
this.alertMessage = "Please select a file";
this.showAlert = true;
} else {
this.disableBtn = true;
this.$parent.showLoading();
let requestUrl =
this.baseUrl + "/media";
let formData = new FormData();
formData.append("file", this.file,);
formData.append("mediatype", this.Mediatype);
let headers = {
headers: {
"Content-Type": "multipart/form-data"
}
};
this.$http
.post(requestUrl, formData, headers)
.then(response => {
this.alertClass = "alert alert-success";
this.alertMessage = response.data.message;
this.$refs.file.value = ''
this.showAlert = true;
this.$parent.hideLoading();
this.disableBtn = false;
this.$parent.getGallery();
})
.catch(() => {
this.disableBtn = false;
this.$parent.hideLoading();
this.alertClass = "alert alert-danger";
this.alertMessage =
"There is a problem in the request.";
this.showAlert = true;
});
}
}

//Follow this instruction
File
<input
type="file"
ref="image2"
v-on:change="handleFilesUpload()"
/>
In methods properties
handleFilesUpload() {
let uploadedFiles = this.$refs.image2.files;
let fileExtension = uploadedFiles[0].name.replace(/^.*\./, "");
//console.log("fileExtension", fileExtension);
let allowedExtensions = /(\.jpg|\.JPG|\.jpeg|\.JPEG|\.png|\.PNG|\.pdf|\.PDF|\.doc|\.docx)$/i;
if (!allowedExtensions.exec(uploadedFiles[0].name)) {
var message = "You can upload jpg, jpeg, png, pdf and docx file only";
this.$refs.image2.value = "";
this.documentFiles = [];
} else {
//console.log("uploadedFiles[i] = ", uploadedFiles[0]);
//Upload for single file
this.documentFiles = uploadedFiles;
//Upload for multiple file
/*
for (var i = 0; i < uploadedFiles.length; i++) {
this.documentFiles.push(uploadedFiles[i]);
}
*/
}
},
// After submit form
validateBeforeSubmit(e) {
let formData = new FormData();
for (var i = 0; i < this.documentFiles.length; i++) {
let file = this.documentFiles[i];
formData.append("files[" + i + "]", file);
}
axios.post('laravel-api-url', formData)
.then(res => {
console.log({res});
}).catch(err => {
console.error({err});
});
},
--In Laravel controller function--
public function save($request){
$total = #count($_FILES['files']['name']);
if ($total>0)
{
$allFiles = $this->uploadFiles($request);
$data['document_file_name'] = ($allFiles) ? $allFiles[0] :
$result = CreditMaster::create($data);
if ($result) {
return response()->json(array('status' => 1,
'message' => 'Data saved successfully!'));
} else {
return response()->json(array('status' => 0, 'message' => 'Save failed!'));
}
}
}
public function uploadFiles($request)
{
$storeFileName = [];
$total = #count($_FILES['files']['name']);
$diretory = '/save_directory_name/';
$path = public_path() . $diretory;
$fileForMate = time() . '_';
for ($i = 0; $i < $total; $i++) {
$tmpFilePath = $_FILES['files']['tmp_name'][$i];
if ($tmpFilePath != "") {
$fileName = $fileForMate . $_FILES['files']['name'][$i];
$newFilePath = $path . $fileName;
if (move_uploaded_file($tmpFilePath, $newFilePath)) {
array_push($storeFileName, $diretory.$fileName);
}
else
{
return $_FILES["files"]["error"];
}
}
}
return $storeFileName;
}

Related

How to Fetch Dynamic Id of an object and show data related to it in Laravel

So let me give you an overview of the problem I am facing.
I am working on a project which has handpicked project as shown below on home page.click to see image
So now I want to show all the associated properties with the specific projects like this
click to see image
I have a databse table named "properties" which has a column "project_id". I want this "project_id" to be dynamic . Right Now I am entering the project id manually i.e. 1,2,3,4.
This is the ajax I am using in my blade file
<script>
$(document).ready(function(){
getProperties();
function getProperties() {
$.ajax({
url:'/handpickedproperties/{$id}',
type:'POST',
data:{'property_type':property_type},
success:function(response){
if(response.success){
var str = '';
stringlength = 135;
if($( window ).width()<1363)
var stringlength = 100;
$.each(response.data, function(k,v){
description = v.description;
if (description.length > stringlength)
description = description.substr(0,stringlength)+'.....more';
photos = JSON.parse(v.images);
if(jQuery.isEmptyObject(photos)){
image_count = 0;
cover_photo = '/images/default/photo not available.png';
}
else{
image_count = Object.keys(photos['withPhotos']).length;
cover_photo = photos.coverPhoto;
}
area_conversion = JSON.parse(v.area_conversions);
str += '<div class="property-box" data-aos="fade-up">\
<div class="pic-area">\
<figure><img src="'+cover_photo+'">\
<span class="photo-count">\
<img src="images/photos-icon.png">'+image_count+'</span>\
</figure>\
</div>\
<div class="listing-property-details">\
<a href="/viewPropertyDetails/'+v.id+'">\
<h5>'+v.name+' in '+v.address+'</a><img src="/images/home/heart2.png" id="pic_'+v.id+'"><a href="/viewPropertyDetails/'+v.id+'"></h5>\
<span class="price-box">\
<strong>'+v.currency+v.price+v.price_unit+'</strong>\
'+v.currency+' '+v.price_per_area+'/sq.ftadsds.\
</span>\
<span class="area-box">\
<img src="/images/listing-area.png">\
<strong><span class="sq_ft" style="width:75%;display:inline-block;">'+v.area_per_sq_ft+' sq.ft.</span>\
<span class="sq_mt" style="width:75%;display:none;">'+area_conversion.sq_mt+' sq.mt.</span>\
<span class="sq_yrd" style="width:75%;display:none;">'+area_conversion.sq_yrd+' sq.yrd.</span></strong>\
('+v.carpet_area+') Carpet Area\
</span>\
<span class="bed-box">\
<img src="/images/listing-bed.png">\
<strong>'+v.bedroom+' BHK</strong>\
</span>\
<span class="bath-box">\
<img src="/images/listing-bath.png">\
<strong>'+v.bathroom+' Bath</strong>\
</span>\
<div class="clear"></div>\
<p class="property-brief" style="color:black;">'+description+'</p></a>';
if(v.featured == 1)
str += '<label style="margin-right: 5px;" class="featured-highlight-buttton">Featured</label>';
if(v.verified == 1)
str += '<label style="margin-right: 5px;" class="highlight-buttton">Verified</label>';
if(v.construction_status == 3)
str += '<label style="margin-right: 5px;" class="highlight-buttton">Under Construction</label>';
if(v.construction_status == 4)
str += '<label style="margin-right: 5px;" class="highlight-buttton">Ready to Move</label>';
// <label class="highlight-buttton">Resale</label>
str += '</div>\
<div class="listing-box-bottom">\
<span>\
Posted on '+v.creation_date+' by '+v.created_by+'\
<strong>'+v.posted_by+'</strong>\
</span>\
Contact '+v.posted_by+'\
</div>\
</div>';
});
$('#results_number').empty();
$('#results_number').append(Object.keys(response.data).length);
$('#property-data').append(str);
$('#no-result-found-div').hide();
}
else{
$('#property-data').empty();
$('#results_number').empty();
$('#results_number').append(0);
$('#no-result-found-div').show();
}
}
});
}
});
</script>
enter code here
This is my controller Code
public function handpickedProperties(Request $request,$id)
{
try
{
$data = $request->all();
$validator = Validator::make($data, [
]);
if ($validator->fails())
return $this->sendError("Validation Failure",$validator->errors(),403);
$price_min = 0;
$price_max = 10000000;
$search = PropertyServices::fetchHandpickedProperties($request->search,$request->type,$request->city,$request->property_type,$price_max,$price_min,$id);
if(is_array($search))
return view('site.handpicked-properties');
else if($search == 0)
return $this->sendError('Property not found',[],206);
else if($search == null)
return $this->sendError('Property not found. Due to Exception in Service',[],500);
}
catch(Exception $e)
{
Log::error('Exception encountered. PropertyController->searchProperties Exception: '.$e);
return $this->sendError('Property not found. Due to Exception.',[],500);
}
}
enter code here
This is the code I have used in Service
public function fetchHandpickedProperties($search,$type,$city,$property_type,$price_max,$price_min,$id)
{
{
try
{
$result = Property::where('project_id','2')
->select('description','images','id','address','name','currency','price','price_unit','price_per_area','area_per_sq_ft','area_conversions','bedroom','bathroom','created_by','posted_by',\DB::raw('DATE_FORMAT(created_at,"%M-%d-%Y") as creation_date'),'carpet_area','verified','construction_status','featured','lat','long')
->get()->toArray();
return $result;
}
catch(Exception $e)
{
Log::error('Exception encountered. PropertyServices->searchProperty Exception: '.$e);
return $result = null;
}
}
}

Following CSV file script are not working properly in Laravel 6

I used following script in my rout file.
Route::get('/tasks', 'appointmentController#exportCsv')
I used the following Js script Html Clickable button in blade template
<script>
function exportTasks(_this) {
let _url = $(_this).data('href');
window.location.href = _url;
}
</script>
<span data-href="./tasks" id="export" class="btn btn-success btn-sm" onclick="exportTasks(event.target);">Export Client Email</span>
I used the following script in my controller. I cant find where is the script problem. CSV file are not download include with the data.
public function exportCsv(Request $request)
{
$fileName = 'tasks.csv';
$tasks = Clientinformation::all();
$headers = array(
"Content-type" => "text/csv",
"Content-Disposition" => "attachment; filename=$fileName",
"Pragma" => "no-cache",
"Cache-Control" => "must-revalidate, post-check=0, pre-check=0",
"Expires" => "0"
);
$columns = array('customerName', 'email', 'cell1', 'cell2', 'landNumber');
$callback = function() use($tasks, $columns) {
$file = fopen('php://output', 'w');
fputcsv($file, $columns);
foreach ($tasks as $task) {
$row['customerName'] = $task->customerName;
$row['email'] = $task->assign->email;
$row['cell1'] = $task->cell1;
$row['cell2'] = $task->cell2;
$row['landNumber'] = $task->landNumber;
fputcsv($file, array($row['customerName'], $row['email'], $row['cell1'], $row['cell2'], $row['cell2'], $row['landNumber'] ));
}
fclose($file);
};
return response()->stream($callback, 200, $headers);
}

ajax does not redirect after controller is called mvc 5

I've got this issue, when ajax is submitted and data is passed to the controller no redirection happens(There is no error as well!), as a matter of fact I know ajax is not a good choice for these type of work(redirecting and full page post back) but actually since I am using localstorage json and I want to post back this json data, I chose this method, any help and hint is most welcome.
here is the view:
<div class="row">
<div class="col-xs-3">
<div id="smartcart" hidden></div>
<div class="col-md-6">
<select name="brandList" id="brandList" class="form-control">
#if (Model != null)
{
foreach (var subbrand in Model.Subbrands.Distinct())
{
foreach (var brand in Model.selectedBrands)
{
if (subbrand.Title != brand)
{
<option>#subbrand.Title</option>
}
}
}
}
</select>
</div>
<div class="col-xs-1">
<button type="submit" command="submit" class="btn btn-success" id="btna" value="compare">
compare with this brand
</button>
<script>
$("#btna").click(function (evt) {
var temp = localStorage.getItem("cart");
var value = $('#brandList option:selected').val();
var cart_list = $.ajax({
type: 'POST',
dataType: "json",
data: { cart_list: temp, brandList: value },
url: '#Url.Action("CompareItemsList", "Compare")'
});
});
</script>
</div>
</div>
and here is the controller:
[HttpPost]
[OutputCache(Duration = 20, VaryByParam = "none")]
public async Task<ActionResult> CompareItemsList(string cart_list, string brandList)
{
var data = JsonConvert.DeserializeObject<List<CartTemplate>>(cart_list);
var equivalantCart = new List<CartTemplate> { };
foreach (var item in data)
{
int temp = Convert.ToInt32(item.product_commonCode);
List<Item> found = db.Items.Where(common => common.CommonCode == temp)
.Where(b => b.Brand == brandList)
.Where(code=>code.Code != item.product_id)
.ToList();
if (found.Count() == 0)
{
var emptyCartItem = new CartTemplate
{
product_commonCode = "0",
product_desc = "-",
product_unit = "-",
product_discount1 = "-",
product_discount2 = "-",
product_discount3 = "-",
product_discount_limit1 = "0",
product_discount_limit2 = "0",
product_discount_limit3 = "0",
product_guarantee = "-",
product_quantity = "0",
product_id = "0",
product_name = item.product_name + " " + item.product_size + " " + "no equivalant exists",
product_price = "0",
product_size = "-",
product_brand = brandList
};
equivalantCart.Add(emptyCartItem);
}
else
{
for (int i = 0; i < found.Count(); i++)
{
var cartItem = new CartTemplate
{
product_commonCode = found[i].CommonCode.ToString(),
product_desc = found[i].Description,
product_discount1 = found[i].DiscountLimit1.ToString(),
product_discount2 = found[i].DiscountLimit2.ToString(),
product_discount3 = found[i].DiscountLimit3.ToString(),
product_quantity = item.product_quantity,
product_id = found[i].Code,
product_name = found[i].ItemName,
product_price = found[i].Price.ToString(),
product_size = found[i].Size.ToString(),
product_brand = found[i].Brand,
product_discount_limit1 = found[i].Limit1.ToString(),
product_discount_limit2 = found[i].Limit2.ToString(),
product_discount_limit3 = found[i].Limit3.ToString()
};
equivalantCart.Add(cartItem);
}
}
}
var obj = new HomePageViewModel
{
ItemTypes = await db.ItemTypes.ToListAsync(),
Categories = await db.Categories.ToListAsync(),
Subcategories = await db.Subcategories.ToListAsync(),
LogoViewModel = await db.LogoViewModels.ToListAsync(),
Brands = await db.Brands.ToListAsync(),
Subbrands = await db.Subbrands.ToListAsync(),
ContactUsViewModel = await db.ContactUsViewModels.ToListAsync(),
selectedBrands = data.Select(s=>s.product_subbrand).Distinct().ToList(),
Cart = equivalantCart
};
});
return View("CompareItemsList",obj);
}
so simply put! Help me, I'm going crazy since I have almost the same thing somewhere else in this project and it works but the difference is that the view is a bootstrap 3 modal there!
I used to do it like this but suddenly it stopped working and I get null in my controller:
<div class="col-xs-3">
#using (Html.BeginForm("CompareItemsList", "Compare", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div id="smartcart" hidden></div>
<div class="col-md-6">
<select name="brandList" id="brandList" class="form-control">
#foreach (var item in Model.Subbrands)
{
foreach (var item2 in Model.selectedBrands)
{
if (item.Title != item2)
{
<option>#item.Title</option>
}
}
}
</select>
</div>
<div class="col-xs-1">
<button type="submit" command="submit" class="btn btn-success" id="btna" value="compare">
Compare with this brand
</button>
</div>
<script>
$("#btna").click(function (evt) {
var data = {};
data.CartList = "cart_list";
});
</script>
}
</div>

In fine uploader, how to edit/update S3 file metadata for files that are uploaded in previous sessions?

I have requirement in which user need to edit/update the s3 file metadata that are uploaded in the previous sessions. I have implemented Initial File List, but I need to make file metadata (filename, caption - new field in my case) editable in the display list. Can it be accomplished?
I see edit files feature, but that is limited to before file gets uploaded. Looks like my requirement not easily supported out of the box FU. I have followed below approach.
In template I have a button with text 'Update Caption', which has onclick="captionUpdate()",that will set JS variable(isCaptionUpdate) to true.
Caption update will trigger DeleteFile endpoint except that it will set param data for caption value from text field that is defines in template
In server side code, the process checks for Caption param, and then call function updateObjectWithCaption()
All of the above works seamlessly with following challenges.Please see the screenshot.
When user click on 'Update Caption', it follows DELETE steps and since I am passing Caption param, it updates S3 file. But problem is in the file list, I will see a status text called 'Deleting.....' appears for brief time. How can I change status to 'Updating Caption....' or something similar
Another issue with #1 is that as soon as S3 update, the File in file list gets removed. UI part still thinks that it is DELETE step for some reason, how can I say to UI that it not really delete?
As you can see in the deleteFile section of JS, caption is taken from document.getElementById('caption').value; that means, even if I click 'Update Caption' of 2nd or 3rd or 4th files, it is taking first occurrence of Caption element. How can I get the caption of the specific file ?
Last but not least, how can I show 'Update Caption' button only for previously uploaded file. I do not want show this button on fresh upload.
Sorry for too many question. I could not separate these question as they are all related to S3 file metadata update topic.
Template:
<div class="qq-uploader-selector qq-uploader" qq-drop-area-text="Drop files here">
<div class="qq-total-progress-bar-container-selector qq-total-progress-bar-container">
<div role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" class="qq-total-progress-bar-selector qq-progress-bar qq-total-progress-bar"></div>
</div>
<div class="qq-upload-drop-area-selector qq-upload-drop-area" qq-hide-dropzone>
<span class="qq-upload-drop-area-text-selector"></span>
</div>
<div class="buttons">
<div class="qq-upload-button-selector qq-upload-button">
<div>Select files</div>
</div>
<button type="button" id="trigger-upload-section1" class="btn btn-primary">
<i class="icon-upload icon-white"></i> Upload
</button>
</div>
<span class="qq-drop-processing-selector qq-drop-processing">
<span>Processing dropped files...</span>
<span class="qq-drop-processing-spinner-selector qq-drop-processing-spinner"></span>
</span>
<ul class="qq-upload-list-selector qq-upload-list" aria-live="polite" aria-relevant="additions removals">
<li>
<div class="qq-progress-bar-container-selector">
<div role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" class="qq-progress-bar-selector qq-progress-bar"></div>
</div>
<span class="qq-upload-spinner-selector qq-upload-spinner"></span>
<img class="qq-thumbnail-selector" qq-max-size="100" qq-server-scale>
<span class="qq-upload-file-selector qq-upload-file"></span>
<span class="qq-edit-filename-icon-selector qq-edit-filename-icon qq-editable" aria-label="Edit filename"></span>
<input class="qq-edit-filename-selector qq-edit-filename" tabindex="0" type="text">
<span class="qq-upload-caption-selector qq-upload-caption"></span>
<span class="qq-edit-caption-icon-selector qq-edit-caption-icon qq-editable" aria-label="Edit caption"></span>
<input class="qq-edit-caption-selector qq-edit-caption qq-editing" placeholder="Caption here ..." tabindex="0" type="text" id="caption">
<span class="qq-upload-size-selector qq-upload-size"></span>
<button type="button" class="qq-btn qq-upload-cancel-selector qq-upload-cancel">Cancel</button>
<button type="button" class="qq-btn qq-upload-retry-selector qq-upload-retry">Retry</button>
<button type="button" class="qq-btn qq-upload-delete-selector qq-upload-delete">Delete</button>
<button type="button" class="qq-btn qq-upload-delete-selector qq-upload-delete" onclick="captionUpdate();">Update Caption</button>
<span role="status" class="qq-upload-status-text-selector qq-upload-status-text"></span>
</li>
</ul>
<dialog class="qq-alert-dialog-selector">
<div class="qq-dialog-message-selector"></div>
<div class="qq-dialog-buttons">
<button type="button" class="qq-cancel-button-selector">Close</button>
</div>
</dialog>
<dialog class="qq-confirm-dialog-selector">
<div class="qq-dialog-message-selector"></div>
<div class="qq-dialog-buttons">
<button type="button" class="qq-cancel-button-selector">No</button>
<button type="button" class="qq-ok-button-selector">Yes</button>
</div>
</dialog>
<dialog class="qq-prompt-dialog-selector">
<div class="qq-dialog-message-selector"></div>
<input type="text">
<div class="qq-dialog-buttons">
<button type="button" class="qq-cancel-button-selector">Cancel</button>
<button type="button" class="qq-ok-button-selector">Ok</button>
</div>
</dialog>
</div>
JS
var isCaptionUpdate = false;
function captionUpdate(){
isCaptionUpdate = true;
};
var manualUploaderSection1 = new qq.s3.FineUploader({
element: document.getElementById('fine-uploader-manual-trigger-section1'),
template: 'qq-template-manual-trigger-section1',
autoUpload: false,
debug: true,
request: {
endpoint: "http://xx_my_bucket_xx.s3.amazonaws.com",
accessKey: "AKIAIAABIA",
},
signature: {
endpoint: "http://localhost/app/ci/php-s3-server/endpoint-cors.php"
},
uploadSuccess: {
endpoint: "http://localhost/app/ci/php-s3-server/endpoint-cors.php?success",
params: {
isBrowserPreviewCapable: qq.supportedFeatures.imagePreviews
}
},
session: {
endpoint: "http://localhost/app/ci/php-s3-server/endpoint-cors.php?filelist"
},
iframeSupport: {
localBlankPagePath: "success.html"
},
cors: {
expected: true
},
chunking: {
enabled: true
},
resume: {
enabled: true
},
deleteFile: {
enabled: true,
method: "POST",
endpoint: "http://localhost/app/ci/php-s3-server/endpoint-cors.php",
params: {
caption: function() {
if (isCaptionUpdate === true) {
isCaptionUpdate = false;
return document.getElementById('caption').value;
}
}
}
},
validation: {
itemLimit: 5,
sizeLimit: 15000000
},
thumbnails: {
placeholders: {
notAvailablePath: "http://localhost/app/ci/s3.fine-uploader/placeholders/not_available-generic.png",
waitingPath: "http://localhost/app/ci/s3.fine-uploader/placeholders/waiting-generic.png"
}
},
callbacks: {
onComplete: function(id, name, response) {
var previewLink = qq(this.getItemByFileId(id)).getByClass('preview-link')[0];
if (response.success) {
previewLink.setAttribute("href", response.tempLink)
}
},
onUpload: function(id, fileName) {
var caption = document.getElementById('caption').value;
this.setParams({'caption':caption});
}
}
});
qq(document.getElementById("trigger-upload-section1")).attach("click", function() {
manualUploaderSection1.uploadStoredFiles();
});
Server side code:
require '/vendor/autoload.php';
use Aws\S3\S3Client;
$clientPrivateKey = 'LB7r54Rgh9sCuTAC8V5F';
$serverPublicKey = 'AKIAU2ZEQ';
$serverPrivateKey = '8Xu6lxcDfKifHfn4pdELnM1E';
$expectedBucketName = 'xx_my_bucket_xx';
$expectedHostName = 'http://s3.amazonaws.com'; // v4-only
$expectedMaxSize = 15000000;
$method = getRequestMethod();
// This first conditional will only ever evaluate to true in a
// CORS environment
if ($method == 'OPTIONS') {
handlePreflight();
}
// This second conditional will only ever evaluate to true if
// the delete file feature is enabled
else if ($method == "DELETE") {
handleCorsRequest();
if (isset($_REQUEST['caption'])) {
updateObjectWithCaption();
} else {
deleteObject();
}
}
// This is all you really need if not using the delete file feature
// and not working in a CORS environment
else if ($method == 'POST') {
handleCorsRequest();
// Assumes the successEndpoint has a parameter of "success" associated with it,
// to allow the server to differentiate between a successEndpoint request
// and other POST requests (all requests are sent to the same endpoint in this example).
// This condition is not needed if you don't require a callback on upload success.
if (isset($_REQUEST["success"])) {
verifyFileInS3(shouldIncludeThumbnail());
}
else {
signRequest();
}
}
//filelist - this is to list already uploaded files
else if ($method == 'GET') {
if (isset($_REQUEST["filelist"])) {
getFileList('test/');
}
}
function getFileList($filePrefix) {
global $expectedBucketName;
$objects = getS3Client()->getIterator('ListObjects', array(
//$objects = getS3Client()->ListObjects(array(
'Bucket' => $expectedBucketName,
'Prefix' => $filePrefix //must have the trailing forward slash "/"
));
$object_list = array();
foreach ($objects as $object) {
//echo $object['Key'] . "<br>";
$object_metadata = getHeadObject($expectedBucketName, $object['Key']);
if (isset($object_metadata['Metadata']['qqfilename'])) {
$keyArr = explode("/", $object['Key']);
$posOfLastString = sizeof($keyArr) - 1;
$uuidArry = explode(".", $keyArr[$posOfLastString]);
$link = getTempLink($expectedBucketName, $object['Key']);
$object_new = array();
$object_new['name'] = $object_metadata['Metadata']['qqfilename'];
$object_new['uuid'] = $uuidArry[0];
$object_new['s3Key'] = $object['Key'];
$object_new['size'] = $object['Size'];
$object_new['s3Bucket'] = $expectedBucketName;
$object_new['thumbnailUrl'] = $link;
array_push($object_list, (object)$object_new);
}
}
echo json_encode($object_list);
}
// This will retrieve the "intended" request method. Normally, this is the
// actual method of the request. Sometimes, though, the intended request method
// must be hidden in the parameters of the request. For example, when attempting to
// send a DELETE request in a cross-origin environment in IE9 or older, it is not
// possible to send a DELETE request. So, we send a POST with the intended method,
// DELETE, in a "_method" parameter.
function getRequestMethod() {
global $HTTP_RAW_POST_DATA;
// This should only evaluate to true if the Content-Type is undefined
// or unrecognized, such as when XDomainRequest has been used to
// send the request.
if(isset($HTTP_RAW_POST_DATA)) {
parse_str($HTTP_RAW_POST_DATA, $_POST);
}
if (isset($_REQUEST['_method'])) {
return $_REQUEST['_method'];
}
return $_SERVER['REQUEST_METHOD'];
}
// Only needed in cross-origin setups
function handleCorsRequest()
// If you are relying on CORS, you will need to adjust the allowed domain here.
header('Access-Control-Allow-Origin: http://localhost');
}
// Only needed in cross-origin setups
function handlePreflight() {
handleCorsRequest();
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Allow-Headers: Content-Type');
}
function getS3Client() {
global $serverPublicKey, $serverPrivateKey;
return S3Client::factory(array(
'key' => $serverPublicKey,
'secret' => $serverPrivateKey
));
}
// Only needed if the delete file feature is enabled
function deleteObject() {
getS3Client()->deleteObject(array(
'Bucket' => $_REQUEST['bucket'],
'Key' => $_REQUEST['key']
));
}
function getHeadObject($bucket, $key) {
$object_metadata = getS3Client()->headObject(array('Bucket' => $bucket,'Key' => $key));
$object_metadata = $object_metadata->toArray();
return $object_metadata;
}
function updateObjectWithCaption() {
$bucket = $_REQUEST['bucket'];
$key = $_REQUEST['key'];
$caption = $_REQUEST['caption'];
$object_metadata = getHeadObject($bucket, $key);
$filename = $object_metadata['Metadata']['qqfilename'];
$fileType = getFileType($key);
getS3Client()->copyObject(array(
'Bucket' => $bucket,
'Key' => $key,
'CopySource' => urlencode($_REQUEST['bucket'] . '/' . $key),
'MetadataDirective' => 'REPLACE',
//'CacheControl' => 'max-age=31536000',
//'Expires' => gmdate('D, d M Y H:i:s T', strtotime('+1 years')), // Set EXPIRES and CACHE-CONTROL headers to +1 year (RFC guidelines max.)
'ContentType' => $fileType,
'Metadata'=>array(
'qqcaption' => $caption,
'qqfilename' => $filename,
),
));
}
function getFileType($key) {
$file_parts = pathinfo($key);
$filetype = "";
switch($file_parts['extension'])
{
case "jpg":
$filetype = "image/jpeg";
break;
case "jpeg":
$filetype = "image/jpeg";
break;
case "png":
$filetype = "image/png";
break;
case "gif":
$filetype = "image/gif";
break;
case "tif":
$filetype = "image/tiff";
break;
case "tiff":
$filetype = "image/tiff";
break;
case "bmp":
$filetype = "image/bmp";
break;
}
return $filetype;
}
function signRequest() {
header('Content-Type: application/json');
$responseBody = file_get_contents('php://input');
$contentAsObject = json_decode($responseBody, true);
$jsonContent = json_encode($contentAsObject);
if (!empty($contentAsObject["headers"])) {
signRestRequest($contentAsObject["headers"]);
}
else {
signPolicy($jsonContent);
}
}
function signRestRequest($headersStr) {
$version = isset($_REQUEST["v4"]) ? 4 : 2;
if (isValidRestRequest($headersStr, $version)) {
if ($version == 4) {
$response = array('signature' => signV4RestRequest($headersStr));
}
else {
$response = array('signature' => sign($headersStr));
}
echo json_encode($response);
}
else {
echo json_encode(array("invalid" => true));
}
}
function isValidRestRequest($headersStr, $version) {
if ($version == 2) {
global $expectedBucketName;
$pattern = "/\/$expectedBucketName\/.+$/";
}
else {
global $expectedHostName;
$pattern = "/host:$expectedHostName/";
}
preg_match($pattern, $headersStr, $matches);
return count($matches) > 0;
}
function signPolicy($policyStr) {
$policyObj = json_decode($policyStr, true);
if (isPolicyValid($policyObj)) {
$encodedPolicy = base64_encode($policyStr);
if (isset($_REQUEST["v4"])) {
$response = array('policy' => $encodedPolicy, 'signature' => signV4Policy($encodedPolicy, $policyObj));
}
else {
$response = array('policy' => $encodedPolicy, 'signature' => sign($encodedPolicy));
}
echo json_encode($response);
}
else {
echo json_encode(array("invalid" => true));
}
}
function isPolicyValid($policy) {
global $expectedMaxSize, $expectedBucketName;
$conditions = $policy["conditions"];
$bucket = null;
$parsedMaxSize = null;
for ($i = 0; $i < count($conditions); ++$i) {
$condition = $conditions[$i];
if (isset($condition["bucket"])) {
$bucket = $condition["bucket"];
}
else if (isset($condition[0]) && $condition[0] == "content-length-range") {
$parsedMaxSize = $condition[2];
}
}
return $bucket == $expectedBucketName && $parsedMaxSize == (string)$expectedMaxSize;
}
function sign($stringToSign) {
global $clientPrivateKey;
return base64_encode(hash_hmac(
'sha1',
$stringToSign,
$clientPrivateKey,
true
));
}
function signV4Policy($stringToSign, $policyObj) {
global $clientPrivateKey;
foreach ($policyObj["conditions"] as $condition) {
if (isset($condition["x-amz-credential"])) {
$credentialCondition = $condition["x-amz-credential"];
}
}
$pattern = "/.+\/(.+)\\/(.+)\/s3\/aws4_request/";
preg_match($pattern, $credentialCondition, $matches);
$dateKey = hash_hmac('sha256', $matches[1], 'AWS4' . $clientPrivateKey, true);
$dateRegionKey = hash_hmac('sha256', $matches[2], $dateKey, true);
$dateRegionServiceKey = hash_hmac('sha256', 's3', $dateRegionKey, true);
$signingKey = hash_hmac('sha256', 'aws4_request', $dateRegionServiceKey, true);
return hash_hmac('sha256', $stringToSign, $signingKey);
}
function signV4RestRequest($rawStringToSign) {
global $clientPrivateKey;
$pattern = "/.+\\n.+\\n(\\d+)\/(.+)\/s3\/aws4_request\\n(.+)/s";
preg_match($pattern, $rawStringToSign, $matches);
$hashedCanonicalRequest = hash('sha256', $matches[3]);
$stringToSign = preg_replace("/^(.+)\/s3\/aws4_request\\n.+$/s", '$1/s3/aws4_request'."\n".$hashedCanonicalRequest, $rawStringToSign);
$dateKey = hash_hmac('sha256', $matches[1], 'AWS4' . $clientPrivateKey, true);
$dateRegionKey = hash_hmac('sha256', $matches[2], $dateKey, true);
$dateRegionServiceKey = hash_hmac('sha256', 's3', $dateRegionKey, true);
$signingKey = hash_hmac('sha256', 'aws4_request', $dateRegionServiceKey, true);
return hash_hmac('sha256', $stringToSign, $signingKey);
}
// This is not needed if you don't require a callback on upload success.
function verifyFileInS3($includeThumbnail) {
global $expectedMaxSize;
$bucket = $_REQUEST["bucket"];
$key = $_REQUEST["key"];
// If utilizing CORS, we return a 200 response with the error message in the body
// to ensure Fine Uploader can parse the error message in IE9 and IE8,
// since XDomainRequest is used on those browsers for CORS requests. XDomainRequest
// does not allow access to the response body for non-success responses.
if (isset($expectedMaxSize) && getObjectSize($bucket, $key) > $expectedMaxSize) {
// You can safely uncomment this next line if you are not depending on CORS
header("HTTP/1.0 500 Internal Server Error");
deleteObject();
echo json_encode(array("error" => "File is too big!", "preventRetry" => true));
}
else {
$link = getTempLink($bucket, $key);
$response = array("tempLink" => $link);
if ($includeThumbnail) {
$response["thumbnailUrl"] = $link;
}
echo json_encode($response);
}
}
// Provide a time-bombed public link to the file.
function getTempLink($bucket, $key) {
$client = getS3Client();
$url = "{$bucket}/{$key}";
$request = $client->get($url);
return $client->createPresignedUrl($request, '+15 minutes');
}
function getObjectSize($bucket, $key) {
$objInfo = getS3Client()->headObject(array(
'Bucket' => $bucket,
'Key' => $key
));
return $objInfo['ContentLength'];
}
// Return true if it's likely that the associate file is natively
// viewable in a browser. For simplicity, just uses the file extension
// to make this determination, along with an array of extensions that one
// would expect all supported browsers are able to render natively.
function isFileViewableImage($filename) {
$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
$viewableExtensions = array("jpeg", "jpg", "gif", "png", "tif", "tiff");
return in_array($ext, $viewableExtensions);
}
// Returns true if we should attempt to include a link
// to a thumbnail in the uploadSuccess response. In it's simplest form
// (which is our goal here - keep it simple) we only include a link to
// a viewable image and only if the browser is not capable of generating a client-side preview.
function shouldIncludeThumbnail() {
$filename = $_REQUEST["name"];
$isPreviewCapable = $_REQUEST["isBrowserPreviewCapable"] == "true";
$isFileViewableImage = isFileViewableImage($filename);
return !$isPreviewCapable && $isFileViewableImage;
}
When user click on 'Update Caption', it follows DELETE steps and since I am passing Caption param, it updates S3 file. But problem is in the file list, I will see a status text called 'Deleting.....' appears for brief time. How can I change status to 'Updating Caption....' or something similar
There are various text options you can set for the delete file feature, such as deleteFile.deletingStatusText.
As you can see in the deleteFile section of JS, caption is taken from document.getElementById('caption').value; that means, even if I click 'Update Caption' of 2nd or 3rd or 4th files, it is taking first occurrence of Caption element. How can I get the caption of the specific file ?
Your markup/template is flawed in that you will end up with multiple elements with an ID of "caption". This is not allowed in HTML. You'll need to restructure your markup accordingly. An ID is not appropriate here. Instead, use a class. You can always get the container element for a file using Fine Uploader's getItemByFileId API method. With this, you can query the descendant elements to look for one with a specific attribute.
Last but not least, how can I show 'Update Caption' button only for previously uploaded file. I do not want show this button on fresh upload.
Files submitted by the user (non-canned/initial files) will result in a call to your onSubmitted callback handler after they are represented in the DOM. At this point, you can use the previously mentioned getItemByFileId to retrieve the container element and hide the button.

Adding a picture to the MEAN.JS sample with Angular-file-upload

I am using MEAN.JS (https://github.com/meanjs/mean) and angular-file-upload (https://github.com/danialfarid/angular-file-upload).
The "Article" sample provided by MEAN.JS contains two fields named "title" and "content". I want to modify that and add a "picture" field allowing a user to upload a picture.
I understand that I have to modify 3 files in MEAN.JS:
~myproject/app/models/article.server.model.js
~myproject/public/modules/articles/controllers/articles.client.controller.js
~myproject/public/modules/articles/views/create-article.client.view.html
However, I can not modify them successfully.
My solution uses angular-file-upload on the client and uses connect-multiparty to handle the file upload.
The images are stored directly in the database which limits their size. I have not included the required code to check the image size.
set up
bower install ng-file-upload --save
bower install ng-file-upload-shim --save
npm i connect-multiparty
npm update
all.js
add angular-file-upload scripts
...
'public/lib/ng-file-upload/FileAPI.min.js',
'public/lib/ng-file-upload/angular-file-upload-shim.min.js',
'public/lib/angular/angular.js',
'public/lib/ng-file-upload/angular-file-upload.min.js',
...
config.js
Inject angular-file-upload dependency
...
var applicationModuleVendorDependencies = ['ngResource', 'ngAnimate', 'ui.router', 'ui.bootstrap', 'ui.utils', 'angularFileUpload'];
...
article.client.controller.js
Use angular-file-upload dependency
angular.module('articles').controller('ArticlesController', ['$scope', '$timeout', '$upload', '$stateParams', '$location', 'Authentication', 'Articles',
function($scope, $timeout, $upload, $stateParams, $location, Authentication, Articles) {
$scope.fileReaderSupported = window.FileReader !== null;
// Create new Article
$scope.create = function(picFile) {
console.log('create');
console.log(picFile);
var article = new Articles({
title: this.title,
content: this.content,
image: null
});
console.log(article);
$upload.upload({
url: '/articleupload',
method: 'POST',
headers: {'Content-Type': 'multipart/form-data'},
fields: {article: article},
file: picFile,
}).success(function (response, status) {
$location.path('articles/' + response._id);
$scope.title = '';
$scope.content = '';
}).error(function (err) {
$scope.error = err.data.message;
});
};
$scope.doTimeout = function(file) {
console.log('do timeout');
$timeout( function() {
var fileReader = new FileReader();
fileReader.readAsDataURL(file);
console.log('read');
fileReader.onload = function(e) {
$timeout(function() {
file.dataUrl = e.target.result;
console.log('set url');
});
};
});
};
$scope.generateThumb = function(file) {
console.log('generate Thumb');
if (file) {
console.log('not null');
console.log(file);
if ($scope.fileReaderSupported && file.type.indexOf('image') > -1) {
$scope.doTimeout(file);
}
}
};
}
create-article.client.view.html
update the create view to handle file selection and upload
<section data-ng-controller="ArticlesController">
<div class="page-header">
<h1>New Article</h1>
</div>
<div class="col-md-12">
<form name="articleForm" class="form-horizontal" data-ng-submit="create(picFile)" novalidate>
<fieldset>
<div class="form-group" ng-class="{ 'has-error': articleForm.title.$dirty && articleForm.title.$invalid }">
<label class="control-label" for="title">Title</label>
<div class="controls">
<input name="title" type="text" data-ng-model="title" id="title" class="form-control" placeholder="Title" required>
</div>
</div>
<div class="form-group">
<label class="control-label" for="content">Content</label>
<div class="controls">
<textarea name="content" data-ng-model="content" id="content" class="form-control" cols="30" rows="10" placeholder="Content"></textarea>
</div>
</div>
<div class="form-group">
<label class="control-label" for="articleimage">Article Picture</label>
<div class="controls">
<input id="articleimage" type="file" ng-file-select="" ng-model="picFile" name="file" accept="image/*" ng-file-change="generateThumb(picFile[0], $files)" required="">
<br/>
<img ng-show="picFile[0].dataUrl != null" ng-src="{{picFile[0].dataUrl}}" class="img-thumbnail" height="50" width="100">
<span class="progress" ng-show="picFile[0].progress >= 0">
<div style="width:{{picFile[0].progress}}%" ng-bind="picFile[0].progress + '%'" class="ng-binding"></div>
</span>
<span ng-show="picFile[0].result">Upload Successful</span>
</div>
</div>
<div class="form-group">
<input type="submit" class="btn btn-default" ng-disabled="!articleForm.$valid" ng-click="uploadPic(picFile)">
</div>
<div data-ng-show="error" class="text-danger">
<strong data-ng-bind="error"></strong>
</div>
</fieldset>
</form>
</div>
</section>
view-article.client.view.html
update list view to include image
<img ng-src="data:image/jpeg;base64,{{article.image}}" id="photo-id" width="200" height="200"/>
list-articles.client.view.html
update view to include imgae
<img ng-src="data:image/jpeg;base64,{{article.image}}" id="photo-id" width="40" height="40"/>
article.server.model.js
Add image to database model
...
image: {
type: String,
default: ''
},
...
article.server.routes.js
Add new route for upload use connect-multiparty
...
multiparty = require('connect-multiparty'),
multipartyMiddleware = multiparty(),
...
app.route('/articleupload')
.post(users.requiresLogin, multipartyMiddleware, articles.createWithUpload);
...
article.server.controller.js
Handle new route for upload require fs
...
fs = require('fs'),
...
/**
* Create a article with Upload
*/
exports.createWithUpload = function(req, res) {
var file = req.files.file;
console.log(file.name);
console.log(file.type);
console.log(file.path);
console.log(req.body.article);
var art = JSON.parse(req.body.article);
var article = new Article(art);
article.user = req.user;
fs.readFile(file.path, function (err,original_data) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
}
// save image in db as base64 encoded - this limits the image size
// to there should be size checks here and in client
var base64Image = original_data.toString('base64');
fs.unlink(file.path, function (err) {
if (err)
{
console.log('failed to delete ' + file.path);
}
else{
console.log('successfully deleted ' + file.path);
}
});
article.image = base64Image;
article.save(function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.json(article);
}
});
});
};
...
Thanks Charlie Tupman for this last solution which works very well.
I simply add the dependencies :
...
multiparty = require('multiparty'),
uuid = require('uuid'),
...
before the exports function and changed two lines to improve the behavior:
...
var destPath = './public/uploads/' + fileName;
article.image = '/uploads/' + fileName;
...
which resolve in:
exports.createWithUpload = function(req, res) {
var form = new multiparty.Form();
form.parse(req, function(err, fields, files) {
var file = req.files.file;
console.log(file.name);
console.log(file.type);
console.log(file.path);
console.log(req.body.article);
var art = JSON.parse(req.body.article);
var article = new Article(art);
article.user = req.user;
var tmpPath = file.path;
var extIndex = tmpPath.lastIndexOf('.');
var extension = (extIndex < 0) ? '' : tmpPath.substr(extIndex);
var fileName = uuid.v4() + extension;
var destPath = './public/uploads/' + fileName;
article.image = '/uploads/' + fileName;
var is = fs.createReadStream(tmpPath);
var os = fs.createWriteStream(destPath);
if(is.pipe(os)) {
fs.unlink(tmpPath, function (err) { //To unlink the file from temp path after copy
if (err) {
console.log(err);
}
});
article.save(function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp(article);
}
});
} else
return res.json('File not uploaded');
});
};
My working solution in MEAN.js
server model:
image:{
type: String,
default: ''
},
server controller:
var item = new Item(JSON.parse(req.body.item));
item.user = req.user;
if(req.files.file)
item.image=req.files.file.name;
else
item.image='default.jpg';
//item.image=
item.save(function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp(item);
}
});
server route: (requires multer: "npm install multer --save")
var multer = require('multer');
app.use(multer({ dest: './public/uploads/'}));
frontend angular controller:
$scope.image='';
$scope.uploadImage = function(e){
console.log(e.target.files[0]);
$scope.image=e.target.files[0];
};
// Create new Item
$scope.create = function() {
// Create new Item object
var item = new Items ({
name: this.name,
bought: this.bought,
number: this.number,
description: this.description,
warranty: this.warranty,
notes: this.notes
});
ItemsService.saveItem(item,$scope.image);
};
service which send's the request:
.factory('ItemsService', ['$http','$rootScope', function($http, $rootScope)
{
var service={};
service.saveItem = function(item, image)
{
var fd = new FormData();
fd.append('file', image);
fd.append('item', JSON.stringify(item));
$http.post('items/', fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})
.success(function(){
console.log('success add new item');
})
.error(function(e){
console.log('error add new item', e);
});
};
return service;
}
]);
html view:
<div class="form-group">
<label class="control-label" for="name">Image</label>
<div class="controls">
<input type="file" data-ng-model="image" id="image" my-file-upload="uploadImage" required>
{{selectedFile.name}}
</div>
</div>
#john prunell Thank you so much for this, I finally figured it out (took me over a week but i am now much more comfortable with the stack) I have forked it to get it to upload the file to a folder:
'exports.createWithUpload = function(req, res) {
var form = new multiparty.Form();
form.parse(req, function(err, fields, files) {
var file = req.files.file;
console.log(file.name);
console.log(file.type);
console.log(file.path);
console.log(req.body.article);
var art = JSON.parse(req.body.article);
var article = new Article(art);
article.user = req.user;
var tmpPath = file.path;
var extIndex = tmpPath.lastIndexOf('.');
var extension = (extIndex < 0) ? '' : tmpPath.substr(extIndex);
var fileName = uuid.v4() + extension;
var destPath = './uploads/' + fileName;
article.image = fileName;
var is = fs.createReadStream(tmpPath);
var os = fs.createWriteStream(destPath);
if(is.pipe(os)) {
fs.unlink(tmpPath, function (err) { //To unlink the file from temp path after copy
if (err) {
console.log(err);
}
});
article.save(function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp(article);
}
});
}else
return res.json('File not uploaded');
});
};
What I would like to do now is fork your solution to allow uploading of multiple images in the same way, if you have any insight into how to do this that would be amazing, I will let you know how I get on.