我有一个表单,用户应该可以在一个输入中上传图像,在另一个输入中上传PDF。他们只能上传一个或另一个。我的问题是,由于我添加了PDF字段,用户无法再上传图像。数据库仍在检索图像名称,但图像未上载到服务器。
我在视图中的表单:
<?= $this->Form->create($work, ['class'=> 'worksform', 'enctype'=>'multipart/form-data']) ?>
<?= $this->Html->image('upload.png', ['class' => 'uploadform center']); ?>
<div class="worksformname"><?= $this->request->session()->read('Auth.User.first_name')?> <?= $this->request->session()->read('Auth.User.last_name');?></div>
<fieldset>
<div class= "worksformtitle"><?= $this->Form->control('Title');?></div>
<div class= "worksformcourse"><?= $this->Form->control('course_id', ['options' => $courses]);?></div>
<div class= "worksformunit"><?= $this->Form->control('unit_id', ['options' => $units]); ?></div>
<div class= "worksformdesc" data-container="body"><?= $this->Form->control('description');?></div>
<p class="worksformtext">I want to upload a... </p>
<li class="buttonworks">
<button type="button" class="videoworks"id="videob"> Video Project </button>
<button type="button" class="audioworks" id="audiob"> Audio Project</button>
<button type="button" class="pdfworks" id="pdfb"> PDF File </button>
<button type="button" class="imageworks" id="imageb"> Image File </button> </li>
<div class="worksformvideo d-none" id="videof" data-container="body"><?= $this->Form->control('video_url')?> </div>
<div class="worksformvideo d-none" id="audiof" data-container="body" ><?= $this->Form->control('sound_url');?> </div>
<div class="worksformvideo d-none"id="pdff"><?= $this->Form->control('pdf', ['type'=> 'file'])?> </div>
<div class="worksformvideo d-none"id="imagef"><?= $this->Form->control('image', ['type'=> 'file'])?> </div>
</fieldset>
<div class="buttonbox">
<?=$this->Form->button(__('Submit')) ?>
</div>
<?= $this->Form->end() ?>
控制器:
public function add()
{
$work = $this->Works->newEntity();
if ($this->request->is('post')) {
$work = $this->Works->patchEntity($work, $this->request->getData());
$work->user_id = $this->Auth->user('id');
$file =$this->request->data['image'];
$name =$this->request->data['image']['name'];
$work->image_url = $name;
$file =$this->request->data['pdf'];
$name1 =$this->request->data['pdf']['name'];
$work->pdf_url = $name1;
if ($this->Works->save($work)) {
move_uploaded_file($file['tmp_name'], WWW_ROOT . 'files/' . $name);
$this->Flash->success(__('The work has been saved.'));
move_uploaded_file($file['tmp_name'], WWW_ROOT . 'files/' . $name1);
$this->Flash->success(__('The work has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The work could not be saved. Please, try again.'));
}
$users = $this->Works->Users->find('list', ['limit' => 200]);
$units = $this->Works->Units->find('list', ['limit' => 200]);
$courses = $this->Works->Courses->find('list', ['limit' => 200]);
$this->set(compact('work', 'users', 'units', 'courses'));
}
视图中的输出:
<div class="worksviewcontainer">
<p class="worksunitcourseview"> <?= $work->course->name?> / <?= $work->unit->name?></p>
<?php if(isset($work->sound_url) && !empty($work->sound_url)): ?>
<iframe width="100%" height="166" scrolling="no" frameborder="no" allow="autoplay" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/<?= h($work->sound_url)?>&color=%23ff5500&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&show_teaser=true"></iframe>
<?php endif; ?>
<?php if(isset($work->video_url) && !empty($work->video_url)): ?>
<div class="embed-responsive embed-responsive-16by9">
<iframe class="embed-responsive-item" src="https://www.youtube.com/embed/<?= h($work->video_url)?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
</div>
<?php endif; ?>
<?php if(isset($work->pdf_url) && !empty($work->pdf_url)): ?>
<object class="pdfviewer" data="<?= '/~i7437085/project/files/'.$work->pdf_url ?>" type="application/pdf" width="600" height="500">
<embed src="<?= '/~i7437085/project/files/'.$work->pdf_url ?>" width="600px" height="500px" />
<p>This browser does not support PDFs. Please download the PDF to view it:
<a href="<?= '/~i7437085/project/files/'.$work->pdf_url ?>">Download PDF</a>.</p>
</embed>
</object>
<?php endif; ?>
<?php if(isset($work->image_url) && !empty($work->image_url)): ?>
<img class="pc" src="<?= '/~i7437085/project/files/'.$work->image_url ?>">
<?php endif; ?>
<p class="workstitleview"> <?= h($work->Title)?> </p>
<p class="worksnameview"> Published by <?= $work->user->first_name?> <?=$work->user->last_name?> (<?= $work->course->name?>) on <?= h($work->created) ?></p>
<p class="descheading">Description</p>
<p class="worksdescview"><?= h($work->description); ?></p>
<div class="commentsheadingcontainer">
<p class="commentsheading">Comments Section</p>
</div>
<div class="workcomments">
<?= $this->Form->create($workComment, ['url' => ['controller' => 'WorkComments', 'action' => 'add']]) ?>
<fieldset>
<div class="row">
<div class="commentsname"><?= $this->request->session()->read('Auth.User.first_name')?> <?= $this->request->session()->read('Auth.User.last_name');?></div>
</div>
<div class="row">
<div class="col-lg-11 nopadding">
<div class="comments-field"><?= $this->Form->control('comment',['placeholder'=>'Type Comment', 'label'=> false ]);?></div>
<?= $this->Form->hidden('work_id', ['value' => $work->id]);?>
</div>
<div class="col-lg-1">
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
</div>
</fieldset>
</div>
答案 0 :(得分:0)
如评论中所述,问题在于:
try:
code...
except comtypes.COMError:
pass
所以Short s3 = new Short("32770");
System.out.println(s3);
总是等于pdf数据。您需要将其更改为:
$file =$this->request->data['image'];
$name =$this->request->data['image']['name'];
$work->image_url = $name;
$file =$this->request->data['pdf'];
$name1 =$this->request->data['pdf']['name'];
$work->pdf_url = $name1;