2017-07-31 87 views
2

我的應用程序中有這種形式,我想添加一個上傳文件字段。我一直在閱讀文檔,當我測試它時,它不會上傳文件。什麼都沒發生。在Symfony 2中上傳一個文件的問題

這裏的實體

/** 
* @var string 
* 
* @ORM\Column(name="mynewpdf", type="string", length=350, nullable=true) 
*/ 
private $mynewpdf; 


/** 
* Set mynewpdf 
* 
* @param string $mynewpdf 
* @return userinfoid 
*/ 
public function getmynewpdf() 
{ 
    return $this->mynewpdf; 
} 


/** 
* Get mynewpdf 
* 
* @return string 
*/ 
public function setmynewpdf($mynewpdf) 
{ 
    $this->mynewpdf = $mynewpdf; 

    return $this; 
} 



public function getPath() 
{ 
    $path = __DIR__.'/../../../../web/newfolder/'; 
    $path .= '/'.$this->mynewpdf; 

    return $path; 
} public function uploadmynewpdf($destinationDirectory) 
{ 
    if (null === $this->mynewpdf) return; 

    $nameFilePdf = uniqid().'.'.$this->mynewpdf->getClientOriginalExtension(); 
    $this->_createdirectory($destinationDirectory); 
    $this->mynewpdf->move($destinationDirectory, $nameFilePdf); 
    $this->setmynewpdf($nameFilePdf); 
} 

private function _createdirectory($destinationDirectory) 
{ 
    $path = __DIR__.'/../../../../web/newfolder/'; 
    if (!file_exists($path)) @mkdir($path, 0777, true); 
    $this->_createFileIndex($path); 

    if (!file_exists($destinationDirectory)) @mkdir($destinationDirectory, 0777, true); 
    $this->_createFileIndex($destinationDirectory);  
} 

private function _createFileIndex($myfolder) 
{ 
    $newfile = $myfolder.'index.html'; 
    $content = "<html><head><title>403 Forbidden</title></head>". 
       "<body>This is Forbidden</body></html>"; 


    if (!file_exists($newfile)) 
    { 
     if (!$handle = @fopen($newfile, 'c')) die("Could not open/create new file"); 
     if (@fwrite($handle, $content) === FALSE) die("Could not write the new file");    
     @fclose($handle); 
    } 
    return true; 
} 

我的控制器:

$file = $userinfoid->getMynewpdf(); 

    if($file != null) 
    { 
    $fileName = md5(uniqid()).'.'.$file->guessExtension(); 
    $file->move(
      $this->container->getParameter('pdf_directory'), 
      $fileName 
     ); 
    $userinfoid->setMynewpdf($fileName);} 

最後形式:

<form action="{{ path('add_document') }}" method="post" > 

<input type="hidden" name="data[userinfoid]" value="{{ data.userinfoid }}" /> 

<div class="span12"> 
      <label>Information about file</label> 
      <p>    
      <textarea id="fileinformation" rows="3" class="span11 campoInvalido" maxlength="" 
         title="Add some information" 
         name="data[fileinformation]" ></textarea> 
      </p>   
      </div>  

    <input type="file" id="mynewpdf" name="data[mynewpdf]" accept="application/pdf 

    </form> 

我不認爲有人形式像它應該被製造。但現在我無法改變它。有人能告訴我怎麼做這項工作?

UPDATE:

首先,不要忘記分享你的代碼問題的關鍵細節。

這是一個Ajax表單。我從來沒有提到這個,應該有。

我的Ajax請求是這樣的:

$.ajax({ 
     type:'POST', 
     url:'page/upload', 
     data: $("form").serialize(), 
     beforeSend:function(){$("#loadingModal").show();},   
     dataType:'json'}) 

上面的代碼不處理文件上傳的,因爲它系列化數據。所以我替換此代碼:

var formData = new FormData($("form")[0]); 
    $.ajax({ 
     type:'POST', 
     url:'page/upload', 
     data: formData, 
     cache: false, 
     contentType: false, 
     processData: false, 
     beforeSend:function(){$("#loadingModal").show();},   
     dataType:'json'}) 

然後在我的控制,我用這個代碼:

$request = $this->getRequest(); 
      $file = $request->files->get('mynewpdf'); 

    // If a file was uploaded 
    if(!is_null($file)){ 
     // generate a random name for the file but keep the extension 

     $filename = uniqid().".".$file->getClientOriginalExtension(); 

     $userinfoid->setMynewpdf($filename); 
     $file->move(
       $this->container->getParameter('pdf_directory'), 
       $filename 
      ); // move the file to a path 

    } 

現在我可以保存在我的Web應用程序文件!

謝謝加布裏埃爾迪茲的幫助!

回答

1

我認爲主要問題是你的形式。在你上傳你的文件併發送給你的動作控制器的時候,沒有任何東西會被傳遞,因爲你的輸入類型文件不像symfony想要的那樣。 你應該看看如何正確上傳文件的symfony文檔,它非常清晰和完整,我認爲你錯過了一些重要的細節。

https://symfony.com/doc/2.8/controller/upload_file.html

,也爲生成,則應該使用symfony的形式幫助它是最好的,做正確,安全地以最快的方式形式。你的情況的產品將是你的實體

$product = new Product(); 
$form = $this->createForm(ProductType::class, $product); 

https://symfony.com/doc/2.8/forms.html

如果你看的文檔上接近您可以在動作控制器此看到。

然後$ form傳遞給視圖。

在createForm方法中,您傳遞了表單的實體,因此symfony知道表單中傳遞了哪種類型的實體。因此,當將建立表格,表格將被提交時會叫你輸入文件到你的財產$ mynewpdf這樣的話聯繫起來:

$userinfoid->getMynewpdf(); 

您的文件將會出現,然後你可以操縱它。

好運