2014-10-27 112 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'); 
    } 
} 
相关问题