Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFR] add an option on maFileField to recover the serverside filename #357

Merged
merged 1 commit into from
Mar 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,18 @@ Define the template to be displayed for fields of type `template` (can be a stri
Give upload information for `file` field type
- `url`: url for server side upload
- `accept`: values allowed by the standard HTML file input accept attribute
- `apifilename`: filename assigned by the server and returned by your API.

If the uploaded file is renamed server-side, you can get the new filename from an api return.

HTTP/1.1 200 OK
Content-Type: application/json
{ "picture_name": "post_12_picture1.jpg"}

you can configure file field as :

nga.field('picture', 'file').uploadInformation({ 'url': 'your_url', 'apifilename': 'picture_name' })

Some other properties are allowed, see https://github.com/danialfarid/angular-file-upload#upload-service for the complete list.

## Reusable Directives
Expand Down
13 changes: 10 additions & 3 deletions src/javascripts/ng-admin/Crud/field/maFileField.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ define(function (require) {

scope.multiple = uploadInformation.hasOwnProperty('multiple') ? uploadInformation.multiple : false;
scope.accept = uploadInformation.hasOwnProperty('accept') ? uploadInformation.accept : '*';
scope.apifilename = uploadInformation.hasOwnProperty('apifilename') ? uploadInformation.apifilename : false;

var files = scope.value ? scope.value.split(',') : [];
scope.files = {};
Expand Down Expand Up @@ -58,7 +59,6 @@ define(function (require) {
for (var file in selectedFiles) {
uploadParams = angular.copy(scope.field().uploadInformation());
uploadParams.file = selectedFiles[file];

$upload
.upload(uploadParams)
.progress(function(evt) {
Expand All @@ -69,10 +69,17 @@ define(function (require) {
})
.success(function(data, status, headers, config) {
scope.files[config.file.name] = {
"name": config.file.name,
"name": scope.apifilename ? data[scope.apifilename] : config.file.name,
"progress": 0
};
scope.value = Object.keys(scope.files).join(',');
if (scope.apifilename) {
var apiNames = Object.keys(scope.files).map(function(fileindex) {
return scope.files[fileindex].name;
});
scope.value = apiNames.join(',');
} else {
scope.value = Object.keys(scope.files).join(',');
}
})
.error(function(data, status, headers, config) {
delete scope.files[config.file.name];
Expand Down