在Unity中上传文件到web服务器时遇到麻烦
本文关键字:服务器 web 遇到 麻烦 文件 Unity | 更新日期: 2023-09-27 18:05:56
我在Unity中上传文件时有问题。我写了一个类,保存屏幕截图到我的本地磁盘,然后通过cgi脚本上传到我的服务器。我没有得到错误,但当我检查上传文件夹在我的web服务器上的文件不存在。我说错了什么?
统一c#
// Saves screenshot as JPG file.
using UnityEngine;
using System.Collections;
using System.IO;
public class CreateJPG : MonoBehaviour
{
public int startX = 0;
public int endX = 1050;
public int startY = 0;
public int endY = 700;
public string screenShotURL= "http://locomoku.com/projects/samnoble/strangecreatures/cgi-bin/upload.cgi";
string fileName;
string filePath;
// Texture2D tex;
public void GrabJPG () {
StartCoroutine(SaveJPG());
}
IEnumerator SaveJPG()
{
// We should only read the screen buffer after rendering is complete
yield return new WaitForEndOfFrame();
// Create a texture the size of the screen, RGB24 format
Texture2D tex = new Texture2D(endX, endY, TextureFormat.RGB24, false);
tex.ReadPixels(new Rect(startX,startY,endX,endY),0,0);
tex.Apply();
// Encode texture into JPG
byte[] bytes = tex.EncodeToJPG(50);
Object.Destroy(tex);
// Get filePrefix from GameSetup array index
GameObject init = GameObject.FindGameObjectWithTag("Initializer");
GameSetup gameSetup = init.GetComponent<GameSetup>();
string prefix = gameSetup.filePrefix;
string subDir = gameSetup.subDir;
string dtString = System.DateTime.Now.ToString("MM-dd-yyyy_HHmmssfff");
fileName = prefix+dtString+".jpg";
filePath = @"/Users/kenmarold/Screenshots/"+subDir+"/";
// If screenshot is > 0 x 0 write bytes
if(endX > 0 && endY > 0)
{
File.WriteAllBytes(filePath+fileName, bytes);
Debug.Log("Your file was saved at " + filePath+fileName);
}
// Create a Web Form
WWWForm form = new WWWForm();
form.AddField("frameCount", Time.frameCount.ToString());
form.AddBinaryData("fileUpload", bytes, fileName, "image/jpg");
// Upload to a cgi script
WWW w = new WWW(screenShotURL, form);
yield return w;
if (!string.IsNullOrEmpty(w.error)) {
print(w.error);
}
else {
print("Finished Uploading " + fileName);
}
}
}
CGI脚本#!/usr/bin/perl -wT
use strict;
use CGI;
use CGI::Carp qw ( fatalsToBrowser );
use File::Basename;
$CGI::POST_MAX = 1024 * 5000;
my $safe_filename_characters = "a-zA-Z0-9_.-";
my $upload_dir = "/home/locomoku/public_html/projects/samnoble/strangecreatures/uploads";
my $query = new CGI;
my $filename = $query->param("photo");
my $email_address = $query->param("email_address");
if ( !$filename )
{
print $query->header ( );
print "There was a problem uploading your photo (try a smaller file).";
exit;
}
my ($name, $path) = fileparse($filename);
$filename = $name;
$filename =~ tr/ /_/;
$filename =~ s/[^$safe_filename_characters]//g;
if ( $filename =~ /^([$safe_filename_characters]+)$/ )
{
$filename = $1;
}
else
{
die "Filename contains invalid characters";
}
my $upload_filehandle = $query->upload("photo");
open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "Can't open $upload_dir/$filename: $!";
binmode UPLOADFILE;
while ( <$upload_filehandle> )
{
print UPLOADFILE;
}
close UPLOADFILE;
print $query->header ( );
这两个程序似乎不匹配。
你的c++程序正在设置CGI参数'frameCount'和'fileUpload'。但是你的Perl程序期望参数叫做'photo'和'email_address'。我不明白这两个程序怎么可能一起工作。
我不能对你的c++代码提供任何帮助,但是你的Perl程序是以一种15年前就过时的方式编写的。
- 我们现在把
-
new CGI
是一种应该尽量避免的语法(因为"new"在Perl中不是关键字——它只是一个方法名)。CGI->new
更安全。 您应该使用
open()
的三参数形式以及词法文件句柄。开放(我upload_fh美元,">","upload_dir/文件名美元")或死"打不开upload_dir/文件名:美元美元!";
use warnings
放在我们的代码中,而不是在shebang行上的"-w"。