【问题标题】:how can I upload an image to server and save its path如何将图像上传到服务器并保存其路径
【发布时间】:2014-10-29 15:07:09
【问题描述】:

我希望能够将图像上传到服务器,但不是将图像作为字节保存在数据库中,而是保存路径,以便在视图页面中通过它的路径调用图像。如果找不到文件夹,则上传它应该上传到某个文件夹中。所有这些都应该使用 MVC5 任何帮助都非常感谢

【问题讨论】:

标签: asp.net-mvc asp.net-mvc-5


【解决方案1】:

不确定您要使用哪种语言来执行此操作,但这是将照片保存到数据库后所需的实现方法。 (基本上就是你说的)

  1. 照片被压缩
  2. 照片发送到服务器
  3. 服务器的 api 处理文件到文件夹的存储
  4. 将文件夹路径+'文件名'保存到数据库中

这是一个 PHP 方法 upload(),是处理步骤 3 和 4 的服务器 API 的一部分

发现于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');
    }
}

【讨论】:

    猜你喜欢
    • 2014-03-18
    • 1970-01-01
    • 2014-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 2011-08-15
    • 2011-09-19
    相关资源
    最近更新 更多