实现下载功能,但出现 404 错误(laravel)

huangapple go评论91阅读模式
英文:

Implementing download functionality but getting a 404 error ( laravel )

问题

我正在尝试在我的代码中实现“下载所有文件”的功能。我已经参考了一些StackOverflow上的链接来实现这个功能。然而,我仍然在努力使其工作,以下是下载控制器,

  1. public function downloadDocuments($id)
  2. {
  3. $documents = DB::table('product_documents')->where('id', $id)->first();
  4. $path2 = $documents->file_path;
  5. $files = array(Storage::url($path2));
  6. $zip = new ZipArchive();
  7. $zip_name = time() . "documents.zip"; // 压缩文件名
  8. $zip->open($zip_name, ZipArchive::CREATE);
  9. foreach ($files as $file) {
  10. echo $path = "public/Product/Documents/" . $file;
  11. if (file_exists($path)) {
  12. $zip->addFromString(basename($path), file_get_contents($path));
  13. } else {
  14. echo "文件不存在";
  15. }
  16. }
  17. $zip->close();
  18. }

这是“下载”按钮和路由,

  1. <Link href="product-documents.download">
  2. <span
  3. className="flex items-center justify-center"
  4. data-tooltip-id="my-tooltip"
  5. data-tooltip-content="下载文件"
  6. >
  7. <DocumentArrowDownIcon className="h-4 w-4" />
  8. </span>
  9. </Link>

路由如下,

  1. Route::get('/download-documents', [ProductsController::class, 'downloadDocuments'])->name('product-documents.download');

以下是存储所有文档/图片的控制器。为了试验,我只想下载多个文档文件。我一遍又一遍地尝试,但它一直失败。请告诉我如何修复这个问题?谢谢

  1. public function store(Request $request)
  2. {
  3. $data = $this->validate($request, [
  4. 'name' => 'required',
  5. 'description' => 'required|max:150',
  6. 'category_id' => 'required',
  7. 'type_id' => 'required',
  8. 'price' => 'required|numeric|min:0|not_in:0',
  9. 'start_date' => 'required',
  10. 'end_date' => 'required',
  11. 'is_active' => 'required',
  12. 'images.*' => 'image|max:2048', // 文件最大尺寸: 2MB
  13. 'documents.*' => 'file|max:2048', // 文件最大尺寸: 2MB
  14. ]);
  15. $product = DB::table('products')
  16. ->insertGetId([
  17. 'name' => $data['name'],
  18. 'description' => $data['description'],
  19. 'type_id' => $data['type_id'],
  20. 'category_id' => $data['category_id'],
  21. 'price' => $data['price'],
  22. 'start_date' => $data['start_date'],
  23. 'end_date' => $data['end_date'],
  24. 'is_password' => $request['is_password'],
  25. 'is_stamping' => $request['is_stamping'],
  26. 'created_at' => now(),
  27. ]);
  28. // 处理图片上传
  29. if ($request->hasFile('images')) {
  30. $i = 1;
  31. foreach ($request->file('images') as $image) {
  32. $name = $request['name'] . '-' . $i;
  33. $now = new DateTime();
  34. $fileName = Str::slug($request['name']) . '-' . $now->format('dmYHis') . '-' . $i . '.' . $image->getClientOriginalExtension();
  35. $path = $image->storeAs('public/Product/Images', $fileName);
  36. DB::table('product_images')->insert([
  37. 'product_id' => $product,
  38. 'name' => $name,
  39. 'file_path' => Storage::url($path),
  40. 'created_at' => now(),
  41. ]);
  42. $i++;
  43. }
  44. }
  45. // 处理文档上传
  46. if ($request->hasFile('documents')) {
  47. $i = 1;
  48. foreach ($request->file('documents') as $document) {
  49. $name = $request['name'] . '-' . $i;
  50. $now = new DateTime();
  51. $fileName = Str::slug($request['name']) . '-' . $now->format('dmYHis') . '-' . $i . '.' . $document->getClientOriginalExtension();
  52. $path = $document->storeAs('public/Product/Documents', $fileName);
  53. DB::table('product_documents')->insert([
  54. 'product_id' => $product,
  55. 'name' => $name,
  56. 'file_path' => Storage::url($path),
  57. 'created_at' => now(),
  58. ]);
  59. $i++;
  60. }
  61. }
  62. $now = Carbon::now();
  63. $month = $now->format('m');
  64. $year = $now->format('Y');
  65. DB::table('log_audits')->insert([
  66. 'module_name' => 'Jualan',
  67. 'description' => 'Simpan Produk ' . $data['name'],
  68. 'user_id' => auth()->user()->id,
  69. 'created_at' => $now,
  70. 'month' => $month,
  71. 'year' => $year,
  72. ]);
  73. return Redirect::route('produk.index');
  74. }

一开始,我尝试实现Zipper/Chumper方法,但它不起作用,因为这个Web应用程序运行在Laravel 7而不是Laravel 5上。然后我进行了一些研究,发现可以使用ZipArchive。在编写代码并尝试运行后,我得到了404错误。我确信我可能在我的代码中漏掉了一些东西,或者可能我错误地编写和引用了它。请帮忙。

英文:

I am trying to implement download all files functionality in my code. I've referred to some links in stackoverflow to implement this. However, I am still struggling to make this works, below are the download controller,

  1. public function downloadDocuments($id)
  2. {
  3. $documents = DB::table(&#39;product_documents&#39;)-&gt;where(&#39;id&#39;, $id)-&gt;first();
  4. $path2 = $documents-&gt;file_path;
  5. $files = array(Storage::url($path2));
  6. $zip = new ZipArchive();
  7. $zip_name = time() . &quot;documents.zip&quot;; // Zip name
  8. $zip-&gt;open($zip_name, ZipArchive::CREATE);
  9. foreach ($files as $file) {
  10. echo $path = &quot;public/Product/Documents/&quot; . $file;
  11. if (file_exists($path)) {
  12. $zip-&gt;addFromString(basename($path), file_get_contents($path));
  13. } else {
  14. echo &quot;file does not exist&quot;;
  15. }
  16. }
  17. $zip-&gt;close();
  18. }

Here is the button and route for the download,

  1. &lt;Link href=&quot;product-documents.download&quot;&gt;
  2. &lt;span
  3. className=&quot;flex items-center justify-center&quot;
  4. data-tooltip-id=&quot;my-tooltip&quot;
  5. data-tooltip-content=&quot;Download dokumen&quot;
  6. &gt;
  7. &lt;DocumentArrowDownIcon className=&quot;h-4 w-4&quot; /&gt;
  8. &lt;/span&gt;
  9. &lt;/Link&gt;
  1. Route::get(&#39;/download-documents&#39;, [ProductsController::class, &#39;downloadDocuments&#39;])-&gt;name(&#39;product-documents.download&#39;);

Below are the controller when storing all the documents/images. For experiment, I only want to download multiple files of the documents. I tried again and again but it keeps on failing. Please tell me how can I fix this ? Thank you

  1. public function store(Request $request)
  2. {
  3. $data = $this-&gt;validate($request, [
  4. &#39;name&#39; =&gt; &#39;required&#39;,
  5. &#39;description&#39; =&gt; &#39;required|max:150&#39;,
  6. &#39;category_id&#39; =&gt; &#39;required&#39;,
  7. &#39;type_id&#39; =&gt; &#39;required&#39;,
  8. &#39;price&#39; =&gt; &#39;required|numeric|min:0|not_in:0&#39;,
  9. &#39;start_date&#39; =&gt; &#39;required&#39;,
  10. &#39;end_date&#39; =&gt; &#39;required&#39;,
  11. &#39;is_active&#39; =&gt; &#39;required&#39;,
  12. &#39;images.*&#39; =&gt; &#39;image|max:2048&#39;, // max file size: 2MB
  13. &#39;documents.*&#39; =&gt; &#39;file|max:2048&#39;, // max file size: 2MB
  14. ]);
  15. $product = DB::table(&#39;products&#39;)
  16. -&gt;insertGetId([
  17. &#39;name&#39; =&gt; $data[&#39;name&#39;],
  18. &#39;description&#39; =&gt; $data[&#39;description&#39;],
  19. &#39;type_id&#39; =&gt; $data[&#39;type_id&#39;],
  20. &#39;category_id&#39; =&gt; $data[&#39;category_id&#39;],
  21. &#39;price&#39; =&gt; $data[&#39;price&#39;],
  22. &#39;start_date&#39; =&gt; $data[&#39;start_date&#39;],
  23. &#39;end_date&#39; =&gt; $data[&#39;end_date&#39;],
  24. &#39;is_password&#39; =&gt; $request[&#39;is_password&#39;],
  25. &#39;is_stamping&#39; =&gt; $request[&#39;is_stamping&#39;],
  26. &#39;created_at&#39; =&gt; now(),
  27. ]);
  28. // handle image uploads
  29. if ($request-&gt;hasFile(&#39;images&#39;)) {
  30. $i = 1;
  31. foreach ($request-&gt;file(&#39;images&#39;) as $image) {
  32. $name = $request[&#39;name&#39;] . &#39;-&#39; . $i;
  33. $now = new DateTime();
  34. $fileName = Str::slug($request[&#39;name&#39;]) . &#39;-&#39; . $now-&gt;format(&#39;dmYHis&#39;) . &#39;-&#39; . $i . &#39;.&#39; . $image-&gt;getClientOriginalExtension();
  35. //$fileName = Str::slug($request[&#39;name&#39;]) . &#39;-&#39; . time() . &#39;.&#39; . $image-&gt;getClientOriginalExtension();
  36. $path = $image-&gt;storeAs(&#39;public/Product/Images&#39;, $fileName);
  37. DB::table(&#39;product_images&#39;)-&gt;insert([
  38. &#39;product_id&#39; =&gt; $product,
  39. &#39;name&#39; =&gt; $name,
  40. &#39;file_path&#39; =&gt; Storage::url($path),
  41. &#39;created_at&#39; =&gt; now(),
  42. ]);
  43. $i++;
  44. }
  45. }
  46. // handle document uploads
  47. if ($request-&gt;hasFile(&#39;documents&#39;)) {
  48. $i = 1;
  49. foreach ($request-&gt;file(&#39;documents&#39;) as $document) {
  50. $name = $request[&#39;name&#39;] . &#39;-&#39; . $i;
  51. $now = new DateTime();
  52. $fileName = Str::slug($request[&#39;name&#39;]) . &#39;-&#39; . $now-&gt;format(&#39;dmYHis&#39;) . &#39;-&#39; . $i . &#39;.&#39; . $document-&gt;getClientOriginalExtension();
  53. //$fileName = Str::slug($request[&#39;name&#39;]) . &#39;-&#39; . time() . &#39;.&#39; . $document-&gt;getClientOriginalExtension();
  54. $path = $document-&gt;storeAs(&#39;public/Product/Documents&#39;, $fileName);
  55. DB::table(&#39;product_documents&#39;)-&gt;insert([
  56. &#39;product_id&#39; =&gt; $product,
  57. &#39;name&#39; =&gt; $name,
  58. &#39;file_path&#39; =&gt; Storage::url($path),
  59. &#39;created_at&#39; =&gt; now(),
  60. ]);
  61. $i++;
  62. }
  63. }
  64. $now = Carbon::now();
  65. $month = $now-&gt;format(&#39;m&#39;);
  66. $year = $now-&gt;format(&#39;Y&#39;);
  67. DB::table(&#39;log_audits&#39;)-&gt;insert([
  68. &#39;module_name&#39; =&gt; &#39;Jualan&#39;,
  69. &#39;description&#39; =&gt; &#39;Simpan Produk &#39; . $data[&#39;name&#39;],
  70. &#39;user_id&#39; =&gt; auth()-&gt;user()-&gt;id,
  71. &#39;created_at&#39; =&gt; $now,
  72. &#39;month&#39; =&gt; $month,
  73. &#39;year&#39; =&gt; $year,
  74. ]);
  75. return Redirect::route(&#39;produk.index&#39;);
  76. }

At first I tried to implement the Zipper/Chumper method but it's not working since this web app run on laravel 7 instead of laravel 5. So then I did some research and found that I can use ZipArchive. After writing the code and tried to run it. I get the 404 error. I am sure that I may have missed something in my code or probably I wrote and referenced it to the wrong things. Please help

答案1

得分: 2

I think there are multiple problems in your code.

First is that the <Link> component doesn't accept a route name but a path.
To use a route name you need to use the Ziggy route() helper.

You can try with :

  1. <Link :href="route('product-documents.download')">
  2. <span
  3. className="flex items-center justify-center"
  4. data-tooltip-id="my-tooltip"
  5. data-tooltip-content="Download dokumen"
  6. >
  7. <DocumentArrowDownIcon className="h-4 w-4" />
  8. </span>
  9. </Link>

or

  1. <Link href="/download-documents)">
  2. <span
  3. className="flex items-center justify-center"
  4. data-tooltip-id="my-tooltip"
  5. data-tooltip-content="Download dokumen"
  6. >
  7. <DocumentArrowDownIcon className="h-4 w-4" />
  8. </span>
  9. </Link>

Second is that currently your downloadDocuments($id) function requires an id which you are not setting (and which you should not need to download all the documents).

英文:

I think there are multiple problems in your code.

First is that the &lt;Link&gt; component doesn't accept a route name but a path.
To use a route name you need to use the ziggy route() helper.

You can try with :

  1. &lt;Link :href=&quot;route(&#39;product-documents.download&#39;)&quot;&gt;
  2. &lt;span
  3. className=&quot;flex items-center justify-center&quot;
  4. data-tooltip-id=&quot;my-tooltip&quot;
  5. data-tooltip-content=&quot;Download dokumen&quot;
  6. &gt;
  7. &lt;DocumentArrowDownIcon className=&quot;h-4 w-4&quot; /&gt;
  8. &lt;/span&gt;
  9. &lt;/Link&gt;

or

  1. &lt;Link href=&quot;/download-documents&#39;)&quot;&gt;
  2. &lt;span
  3. className=&quot;flex items-center justify-center&quot;
  4. data-tooltip-id=&quot;my-tooltip&quot;
  5. data-tooltip-content=&quot;Download dokumen&quot;
  6. &gt;
  7. &lt;DocumentArrowDownIcon className=&quot;h-4 w-4&quot; /&gt;
  8. &lt;/span&gt;
  9. &lt;/Link&gt;

Second is that currently your downloadDocuments($id) function requires an id which you are not setting (and which you should not need to downlaod all the documents).

huangapple
  • 本文由 发表于 2023年4月4日 12:05:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925434.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定