Laravel 5 如何在axios请求中使用Maatwebsite\Excel下载csv文件?
在laravel 5.8/axios“^0.18”、“vue”:“^2.5.17”应用程序中,我使用Maatwebsite\Excel上载csv文件 如果我在GET请求中运行laravel控件,它就会起作用, 但我使用axios进行post请求,运行请求时根本不会下载文件。 我在JS代码中运行我的请求:Laravel 5 如何在axios请求中使用Maatwebsite\Excel下载csv文件?,laravel-5,axios,maatwebsite-excel,Laravel 5,Axios,Maatwebsite Excel,在laravel 5.8/axios“^0.18”、“vue”:“^2.5.17”应用程序中,我使用Maatwebsite\Excel上载csv文件 如果我在GET请求中运行laravel控件,它就会起作用, 但我使用axios进行post请求,运行请求时根本不会下载文件。 我在JS代码中运行我的请求: axios({ method: ( 'post' ), url: this.$store.getters.apiUrl + '/p
axios({
method: ( 'post' ),
url: this.$store.getters.apiUrl + '/personal/run-user-list-export-to-csv',
data: { user_id : user_id, user_list_id: user_list_id },
}).then((response) => {
this.showPopupMessage("User list export", "User list successfully exported !", 'success');
}).catch((error) => {
this.showPopupMessage("User list export", 'Error exporting user\'s list !', 'error');
});
控制:
public function run_user_list_export_to_csv() // http://127.0.0.1:8000/api/run-user-list-export-to-csv
{
$request= request();
...
$ret= \Excel::download(new exportSearchResults($user_list_id), 'file.csv'); // if write RETURN this line file is not downloaded anyway...
return response()->json( ['error_code' => 1, 'message' => '', 'ret' => $ret, HTTP_RESPONSE_INTERNAL_SERVER_ERROR ] );
} // public function storerun_user_list_export_to_csv(UserLisRequest $request)
我看到输出后浏览器的响应:
{"error_code":1,"message":"","ret":{"headers":{}},"0":500}
[2019-06-14 04:46:23] local.INFO: HTTP/1.0 200 OK
Cache-Control: public
Content-Disposition: attachment; filename="aaa Z3 iuy65 .csv"
Date: Fri, 14 Jun 2019 04:46:23 GMT
Last-Modified: Fri, 14 Jun 2019 04:46:23 GMT
但文件并没有下载。正确程度如何
已修改:
我尝试在我的控制操作中通过发送标题进行重拍,如:
public function run_user_list_export_to_csv()
{
$request= request();
...
$filename= $title.'.csv';
$download_path= $filename;
$ret= \Excel::download(new exportSearchResults($user_list_id), $download_path);
\Log::info($ret);
$headers = ['Content-Type: application/csv','Content-Disposition: attachment; filename={$filename}'];
return response($download_path, 200,$headers);
} // public function storerun_user_list_export_to_csv(UserLisRequest $request)
我在日志文件输出中看到:
{"error_code":1,"message":"","ret":{"headers":{}},"0":500}
[2019-06-14 04:46:23] local.INFO: HTTP/1.0 200 OK
Cache-Control: public
Content-Disposition: attachment; filename="aaa Z3 iuy65 .csv"
Date: Fri, 14 Jun 2019 04:46:23 GMT
Last-Modified: Fri, 14 Jun 2019 04:46:23 GMT
但没有下载任何文件。
哪条路对
修改2:
1) 我生成了csv文件,但在浏览器的下载方法中下载失败。我似乎更喜欢这种方式。
但我可以将生成的csv文件移动到某个目录(比如/home/user/)并在客户端的csv应用程序中打开它
2) 如何使用vue js定义当前用户的名称及其下载目录(请记住,客户端可能有不同的操作系统)
修改3:
我找到了这个插件并尝试安装它
$ npm install ynishi/vuecsv
> core-js@2.6.9 postinstall /mnt/_work_sdb8/wwwroot/lar/wiznext/msg-laravel-application/node_modules/core-js
> node scripts/postinstall || echo "ignore"
Thank you for using core-js ( https://github.com/zloirock/core-js ) for polyfilling JavaScript standard library!
The project needs your help! Please consider supporting of core-js on Open Collective or Patreon:
> https://opencollective.com/core-js
> https://www.patreon.com/zloirock
Also, the author of core-js ( https://github.com/zloirock ) is looking for a good job -)
> bootstrap-vue@1.5.1 postinstall /mnt/_work_sdb8/wwwroot/lar/wiznext/msg-laravel-application/node_modules/bootstrap-vue
> opencollective postinstall || exit 0
Thanks for installing bootstrap-vue
Please consider donating to our open collective
to help us maintain this package.
Number of contributors: 227
Number of backers: 31
Annual budget: $774
Current balance: $1,190
Donate: https://opencollective.com/bootstrap-vue/donate
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
+ vuecsv@0.0.10
added 44 packages from 32 contributors and audited 17038 packages in 87.476s
found 1 high severity vulnerability
run `npm audit fix` to fix them, or `npm audit` for details
上面的产出有些不寻常,但它只是一种宣传。
下一步我运行:
npm audit fix
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
up to date in 8.632s
fixed 0 of 1 vulnerability in 17038 scanned packages
1 vulnerability required manual review and could not be updated
在package.json之后:
{
"private": true,
"scripts": {
"dev": "npm run development",
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "npm run development -- --watch",
"watch-poll": "npm run watch -- --watch-poll",
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
"prod": "npm run production",
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
},
"devDependencies": {
"axios": "^0.18",
"bootstrap": "^4.0.0",
"cross-env": "^5.1",
"jquery": "^3.4.1",
"laravel-mix": "^4.0.7",
"lodash": "^4.17.5",
"popper.js": "^1.12",
"resolve-url-loader": "^2.3.1",
"sass": "^1.15.2",
"sass-loader": "^7.1.0",
"vue": "^2.5.17",
"vue-template-compiler": "^2.6.10"
},
"dependencies": {
"cors": "^2.8.5",
"remove": "^0.1.5",
"v-tooltip": "^2.0.2",
"vee-validate": "^2.2.5",
"vee-validate-laravel": "^1.1.0",
"vue-js-modal": "^1.3.31",
"vue-moment": "^4.0.0",
"vue-notification": "^1.3.16",
"vue-router": "^3.0.6",
"vue-select": "^3.1.0",
"vue-slider-component": "^3.0.31",
"vue2-filters": "^0.6.0",
"vuecsv": "github:ynishi/vuecsv",
"vuejs-paginate": "^2.1.0",
"vuex": "^3.1.0"
}
}
我试着用这把小提琴,
但我的文件中引用了错误:
app.js?dt=1560770053:2823 Uncaught ReferenceError: VueCSV is not defined
at Module../node_modules/babel-loader/lib/index.js?!./node_modules/vue-loader/lib/index.js?!./resources/js/components/Horizontal/personal/userLists/list.vue?
在/Horizontal/personal/userLists/list.vue中:
<script>
...
import Vue from 'vue';
Vue.component("csv-download", VueCSV.CsvDownload)
如果我将导入行修改为:
import VueCSV from 'vuecsv'; // /node_modules/vuecsv/dist/vuecsv.min.js
I see error in npm console :
This dependency was not found:
* Vue in ./node_modules/vuecsv/dist/vuecsv.min.js
To install it, you can run: npm install --save Vue
当然我已经安装了vue
哪条路是对的?
难道这个插件不工作?您能提出类似建议吗?使用axios上传文件:
const config = {
headers: {
"content-type": "multipart/form-data",
"Access-Control-Allow-Origin": "*"
}
};
axios.post(ROOT+'/image/upload',data,config);
您的laravel路线:
Route::post('/image/upload','API\ImageController@upload');
您的ImageController:
public function upload(Request $request)
{
$file = $request->file('image');
要使用axios上载文件,请执行以下操作:
const config = {
headers: {
"content-type": "multipart/form-data",
"Access-Control-Allow-Origin": "*"
}
};
axios.post(ROOT+'/image/upload',data,config);
您的laravel路线:
Route::post('/image/upload','API\ImageController@upload');
您的ImageController:
public function upload(Request $request)
{
$file = $request->file('image');
请看修改后的区块请看修改后的2区块请看修改后的3区块请看修改后的4区块请看修改后的区块请看修改后的2区块请看修改后的3区块请看修改后的4区块