首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Unity将图像上传到Codeigniter不起作用的服务器

Unity将图像上传到Codeigniter不起作用的服务器
EN

Stack Overflow用户
提问于 2019-03-30 18:25:48
回答 2查看 570关注 0票数 0

我想上传一个图像jpg到我的服务器。我试过了,但没用。

我不知道上传失败的原因。

在详细代码下面。

统一C#

public void TakePicture(int maxSize)
    {
        if (NativeCamera.IsCameraBusy())
        {
            Debug.Log("Camera Busy");
            return;
        }
        else
        {
            NativeCamera.Permission permission = NativeCamera.TakePicture((path) =>
            {
                Debug.Log("Image path: " + path);
                if (path != null)
                {
                    // Create a Texture2D from the captured image                                        
                    Texture2D texture = NativeCamera.LoadImageAtPath(path, maxSize);
                    //snap.SetPixels(tex.GetPixels());                    

                    byte[] bytes = File.ReadAllBytes(path);
                    Debug.Log("Bytes:" + bytes);
                    Destroy(texture);

                    StartCoroutine(upload_ocr_image(bytes));

                    if (texture == null)
                    {
                        Debug.Log("Couldn't load texture from " + path);
                        return;
                    }

                    // Assign texture to a temporary quad and destroy it after 5 seconds
                    GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
                    quad.transform.position = Camera.main.transform.position + Camera.main.transform.forward * 2.5f;
                    quad.transform.forward = Camera.main.transform.forward;
                    quad.transform.localScale = new Vector3(1f, texture.height / (float)texture.width, 1f);

                    Material material = quad.GetComponent<Renderer>().material;
                    if (!material.shader.isSupported) // happens when Standard shader is not included in the build
                        material.shader = Shader.Find("Legacy Shaders/Diffuse");

                    material.mainTexture = texture;

                    Destroy(quad, 5f);

                    // If a procedural texture is not destroyed manually, 
                    // it will only be freed after a scene change
                    Destroy(texture, 5f);

                }
            }, maxSize);

            Debug.Log("Permission result: " + permission);
        }

    }

    IEnumerator upload_ocr_image(byte[] bytes)
    {
        // UtilityScript.GetComponent<utility>().Sand_Clock_Loading(true);

        // yield return new WaitForSeconds(2.5f);

        WWWForm formDate = new WWWForm();

        formDate.AddField("frameCount", Time.frameCount.ToString());
        formDate.AddBinaryData("ocr_image", bytes, "ocr.jpg", "image/jpg");

        formDate.AddField("phone_code", "+61");
        formDate.AddField("phone_number", "434599859");                     

        using (UnityWebRequest www = UnityWebRequest.Post(web_url.url_upload_ocr_image, formDate))
        {
            yield return www.Send();

            //UtilityScript.GetComponent<utility>().Sand_Clock_Loading(false);

            if (www.isNetworkError)
            {
                Debug.Log(www.error);
               // UtilityScript.GetComponent<utility>().MessageBox_Check_Connection();
            }
            else
            {
                Debug.Log(www.downloadHandler.text);                
            }
        }
    }

,这是服务器codeigniter php.中的代码

function upload_ocr_image()
    {           
        $phone_code = $this->input->post('phone_code', true);
        $phone_number = $this->input->post('phone_number', true);

        $allowedType = array(IMAGETYPE_GIF,IMAGETYPE_JPEG,IMAGETYPE_PNG);
        $imgType = exif_imagetype($_FILES['ocr_image']['tmp_name']);
        if(!in_array($imgType,$allowedType))
        {
            echo "Images Type Error. Images Type Only : GIF , JPEG, PNG";
            exit;
        }
        else
        {
            //upload original size front end slider
            $config['upload_path'] = './assets/ocr_image/';
            $config['allowed_types'] = 'gif|jpg|png|jpeg';
            $config['file_name'] = $phone_code.$phone_number;
            $config['overwrite'] = FALSE;
            $config['max_size'] = '8096';
            $config['max_width']  = '6000';
            $config['max_height']  = '6000';

            $this->load->library('upload', $config);

            if(!$this->upload->do_upload("ocr_image"))
            {
                echo "Maximum File Size Only 2 Mb Or Max Width = 2000 , Height = 2000";
                exit;
            }
            else
            {
                $img_data = $this->upload->data();

                // Create Thumbnail
                /*
                $magicianObj = new imageLib("assets/ocr_image/".$img_data["file_name"].$phone_code.$phone_number);
                $magicianObj -> resizeImage(80, 80, 0, true);
                $magicianObj -> saveImage("assets/admin/img/photos/".$img_data["raw_name"]."_thumb".$img_data["file_ext"], 100);

                $next_id = $next_id.$img_data["file_ext"];
                $thumb_name = $img_data["raw_name"]."_thumb".$img_data["file_ext"];             

                $data = array("photo_name"=>$next_id,
                              "thumb_photo_name"=>$thumb_name,
                              "create_date"=>date("Y-m-d H:i:s"));

                $this->db->insert("gallery_tbl",$data);
                */

            }
        }   
    }

找不到错误。但它总是失败。

在codeigniter PHP中这行代码:

上载if(!$

->upload->do_upload(“ocr_image”))

始终返回true

它是如何工作的?如何以适当的方式上传图片?

编辑:

我使用unity的资源原生相机android和ios从相机拍照。

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-04-01 08:54:48

我终于找到了问题所在。

只需在codeigniter上传文件中将允许的类型扩展设置为排除所有扩展名即可。

更改:

$config['allowed_types'] = 'gif|jpg|png|jpeg';

$config['allowed_types'] = '*';
票数 0
EN

Stack Overflow用户

发布于 2019-03-30 19:05:10

只需在后面添加initialize $this->upload->initialize($config);

$this->load->library(' upload ',$config);并检查,也检查上载目录路径是否正确?和目录权限

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55430482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档