2011-12-27 32 views
1

私は、フォームPHPでAjaxを使用して画像をアップロードするには?

<form id="profile_imageform" class="image_form" enctype="multipart/form-data"> 

    <fieldset> 
     <div class="entry"> 
      <label>Filename:</label> 
      <input type="file" name="file" id="file" /> 
     </div> 
    </fieldset> 
    <div class="actions"> 
     <button type="submit">Save &amp; Close</button>(<a href="#/profile" class="cancel">Cancel</a>) 
    </div> 
</form> 

ProfileForm.prototype.init =機能(){ のvar自己のような私のjsファイルを見て=これを持っています。

//Initialize properties 
    this.view = $("#profile_form_view"); 
    this.imageForm = $("#profile_imageform"); 
    this.fields.id = this.imageForm.find(":input[ name = 'id' ]"); 
    this.fields.image = this.imageForm.find(":input[ name = 'file' ]"); 

    //Bind the submit handler 
    this.imageForm.submit(function(event){ 

     //Submit the form 
     self.saveImage(this.fields.id.val(), this.fields.image.val()); 

     //Cancel default event 
     return(false); 

    }); 

ProfileForm.prototype.saveImage = function(id, image, onSuccess, onError){ 
    var self = this; 

    application.ajax({ 
     url: "test.php", 
     data: { 
      method: "saveImage", 
      id: id, 
      image: image 
     }, 
     success: function(response){ 
      if (response.success){ 
       onSuccess(response.data); 
      }else{ 
       onError(response.errors); 
      } 
     } 

    }); 
}; 

しかし

this.fields.image.val() 

私は必要なものをイメージ名を返すことがtmp_nameだです。 jQueryでtmp_nameを取得できますか?もしそうなら、どのように?

しかし、このPHPコードは、エラー

if (isset($_POST['method'])) 
{ 
    if (isset($_POST['image'])) 
    { 
     echo $_FILES['image']['tmp_name']; 
    } 
} 

答えて

3

いけないあなたが任意のエラーチェックを行ういけない場合、エラー応答を期待を返します....

function file_upload_error_message($error_code) { 
    switch ($error_code) { 
    case UPLOAD_ERR_INI_SIZE: 
     return 'The uploaded file exceeds the upload_max_filesize directive in php.ini'; 
    case UPLOAD_ERR_FORM_SIZE: 
     return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'; 
    case UPLOAD_ERR_PARTIAL: 
     return 'The uploaded file was only partially uploaded'; 
    case UPLOAD_ERR_NO_FILE: 
     return 'No file was uploaded'; 
    case UPLOAD_ERR_NO_TMP_DIR: 
     return 'Missing a temporary folder'; 
    case UPLOAD_ERR_CANT_WRITE: 
     return 'Failed to write file to disk'; 
    case UPLOAD_ERR_EXTENSION: 
     return 'File upload stopped by extension'; 
    default: 
     return 'Unknown upload error'; 
} 
} 
+0

jQueryでその画像の一時名を取得したいとします。どうすればいい? pls help – user1066679

+0

最良の方法は、サーバー側の言語(?)からの応答を取得してから、コールバックに戻して、使用している言語は何ですか? – Philip

+0

申し訳ありません私はあなたに質問をしました.PHP。あなたのフォームは最初に間違って設定されています。あなたのフォーム入力はtype = fileでなければならず、キー(名前)はあなたのイメージです! $ _FILES ['image'] === name = image – Philip

関連する問題