2014-10-27 113 views
-3

我希望能夠將圖像上載到服務器,而不是將圖像作爲字節保存在數據庫中我想保存路徑,以便在視圖頁面中我通過它的path.also圖像上傳它應該被上傳的圖像,如果文件夾沒有找到創建one.all這應該是使用MVC5任何幫助真的很感激如何將圖像上載到服務器並保存路徑

+0

什麼編程語言? – 2014-10-27 13:31:36

+0

MVC 5可用c# – mohammad 2014-10-27 13:38:07

+0

哪種語言/框架?你已經嘗試了什麼?你在哪裏面臨這個問題? – 2014-10-27 13:38:14

回答

1

不確定你想要什麼語言要做到這一點,但這是將照片保存到數據庫的理想實施方法。 (基本上是你的說法)

  1. 照片被壓縮
  2. 照片被髮送到服務器
  3. 服務器的API處理文件存儲到文件夾
  4. 保存FOLDERPATH +「文件名」到數據庫

這裏是一個PHP方法upload(),處理步驟3和4的服務器API的一部分

Found on Ray Wenderlich

//upload API 
function upload($id, $photoData, $title) { 

    // index.php passes as first parameter to this function $_SESSION['IdUser'] 
    // $_SESSION['IdUser'] should contain the user id, if the user has already been authorized 
    // remember? you store the user id there in the login function 
    if (!$id) errorJson('Authorization required'); 

    // check if there was no error during the file upload 
    if ($photoData['error']==0) { 

     // insert the details about the photo to the "photos" table 
     $result = query("INSERT INTO photos(IdUser,title) VALUES('%d','%s')", $id, $title); 
     if (!$result['error']) { 

      // fetch the active connection to the database (it's initialized automatically in lib.php) 
      global $link; 

      // get the last automatically generated ID in the photos table 
      $IdPhoto = mysqli_insert_id($link); 

      // move the temporarily stored file to a convenient location 
      // your photo is automatically saved by PHP in a temp folder 
      // you need to move it over yourself to your own "upload" folder 
      if (move_uploaded_file($photoData['tmp_name'], "upload/".$IdPhoto.".jpg")) { 

       // file moved, all good, generate thumbnail 
       thumb("upload/".$IdPhoto.".jpg", 180); 

       //just print out confirmation to the iPhone app 
       print json_encode(array('successful'=>1)); 
      } else { 
       //print out an error message to the iPhone app 
       errorJson('Upload on server problem'); 
      }; 

     } else { 
      errorJson('Upload database problem.'.$result['error']); 
     } 
    } else { 
     errorJson('Upload malfunction'); 
    } 
} 
相關問題