将纹理 2d 字节上传到 Web 服务器问题


Upload texture 2d bytes to web server issues

这是我在这里的第一篇文章。我需要一些帮助来建立一个古怪的系统。基本上,我所拥有的是嵌入在我的游戏中的注册/用户注册,并且我有一个系统,该系统为使用渲染目标的用户遵守128x128像素大小的头像。

https://www.dropbox.com/s/5vw8i9151uz6zkn/Screenshot%202014-12-08%2021.33.11.png?dl=0

我有一个在线系统,该系统应该接收图像数据,将其保存到文件中,然后将图像分配给登录的用户。但是,当我发送图像数据时,它可以很好地保存到文件中,但由于某种原因不是正确的 png 文件。我使用以下代码将呈现器目标保存到客户端的适当图像文件中

    public static void SaveToGLPng(this Microsoft.Xna.Framework.Graphics.Texture2D texture, Stream s)
    {
        byte[] imageData = new byte[4 * texture.Width * texture.Height];
        texture.GetData<byte>(imageData);
        //Since OpenGL is a special snowflake, switch around the R and B values
        for (int i = 0; i < imageData.Length; i += 4)
        {
            byte temp = imageData[i];   //store r
            imageData[i] = imageData[i+2];//swap r
            imageData[i+2] = temp;//swap b
        }
        int bp = 0;
        System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(texture.Width, texture.Height);
        System.Drawing.Imaging.BitmapData bmData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, texture.Width, texture.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);
        IntPtr pnative = bmData.Scan0;
        System.Runtime.InteropServices.Marshal.Copy(imageData, 0, pnative, 4 * texture.Width * texture.Height);
        bitmap.UnlockBits(bmData);
        bitmap.Save(s, System.Drawing.Imaging.ImageFormat.Png);
    }
}

为了使一切正常,我将它返回的数据保存到内存流中,并将其传递给一个名为WebCall的类,该类实现了异步WebClient功能

要求网络呼叫的类

    public void MakeCall()
    {
        using (WebClient wb = new WebClient())
        {
            UriBuilder b = new UriBuilder(url);
            wb.UploadValuesCompleted += wb_UploadValuesCompleted;
            wb.UploadValuesAsync(b.Uri, paramSet);
        }
    }

构造函数方法

    public static WebCall UploadAvatar(byte[] data, int userID)
    {
        WebCall call = new WebCall();
        call.url = URL;
        call.WriteParam("action", "upload_avatar_game");
        call.WriteParam("game", "true");
        call.WriteParam("userID", userID.ToString());
        call.WriteParam("file_data", Encoding.Default.GetString(data));
        return call;
    }

我在哪里进行网络通话

            MemoryStream pngStream = new MemoryStream();
            captureTarget.SaveToGLPng(pngStream);
            var webCall = Tools.WebCall.UploadAvatar(pngStream.GetBuffer(),infoContext.UserID);
            webCall.OnCallCompleted +=webCall_OnCallCompleted;
            webCall.MakeCall();
            pngStream.Close();

这一切都可以很好地到达服务器,它调用此方法

function upload_avatar_game($params){
    $upload_dir = "../files/avatars/";
    $data = $params['file_data'];
    $dt = new DateTime();
    $dtform = $dt->format("Y-m-d-H.i.s");
    $name = $dtform . ".png";
    $fullpath = $upload_dir . $name;
    if(!isset($params['userID'])){
        echo 'false';
        die();
    }
    $user_id = $params['userID'];
    try {
        $existing_file = FileModel::get_user_avatar($user_id);
        if($existing_file){
            unlink($existing_file->getDownloadPath());
            $existing_file->setIsDeleted(true);
            $existing_file->Save();
        }
        $success = file_put_contents($fullpath,$data);
        if($success){
            $size = filesize($fullpath);
            $file = FileModel::create_file($name,$upload_dir,0,$size,$user_id);
            if($file){
                $fid = $file->getFileID();
                echo 'true';
                die();
            }
        }
    } catch(Exception $ex){
        $GLOBALS['partial'] = '../partials/error.php';
        $GLOBALS['error_msg'] = $ex->getMessage();
    }
}

这是将参数传递给方法的地方。有测试以查看它是否是合法请求,但对"file_data"条目没有执行任何操作

function process_script(){    
    if(!isset($_GET['action']) && !isset($_POST['action']))
    {
        header('Location: ?action=login_user');
    }
    $params = isset($_GET['action']) ? $_GET : $_POST;
    //test for legimate request.
    //after test
    switch($params['action'])
    {
        case "upload_avatar_game":
            if(isset($params['game']))
            {
                upload_avatar_game($params);
            } else {
                upload_avatar($params['fileUpload']);
            }
            break;
        }
    }

它被创建并添加到我的数据库中,但发送的数据都是乱码?我在这里转移是不是做错了什么?

谢谢亚伦·斯图尔特

此链接描述了更简单的方法,该方法以类似于 Web 浏览器的方式利用文件上传:

从 c sharp 客户端应用程序上传到 PHP 服务器

关键是生成图像,将其保存到临时文件中。

然后你把它上传到服务器,从$_FILES抓取它,把它保存在你需要的地方,把它写到数据库,等等。

然后,删除临时文件并继续下一步要执行的操作。

如何处理服务器端的文件上传,您可以在有关如何在 PHP 中上传文件的文档中找到。

我认为答案是删除Encoding.Default.GetString并将字节传递给网络调用。

其他选择可能是您不将表单编码设置为多部分/表单数据,尽管我不确定您是否使用表单。