使用C#上传图像

时间:2014-09-12 16:44:23

标签: c# php

所以问题是POST请求不正确我猜,因此php无法正确读取它。

我将在下面发布php和c#请求代码。 但我认为主要问题是请求的c#代码.. 谢谢你的帮助。

C#代码:

string screenshot = @"C://1478941.jpg";

var request = (HttpWebRequest)WebRequest.Create("https://ADDRESS/screenshots/index.php?");  
string postData = "&access_token=" + label11.Text;
postData += "&screenshot=" + screenshot;
byte[] data = Encoding.ASCII.GetBytes(postData);

request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;

using (Stream stream = request.GetRequestStream())
{
    stream.Write(data, 0, data.Length);
}

var response = (HttpWebResponse)request.GetResponse();

string xml = new StreamReader(response.GetResponseStream()).ReadToEnd();

PHP代码:

$post_access_token = $_POST['access_token'];



$uid = $api->verifyUser($post_access_token);

if (isset($_FILES['screenshot']) && $_FILES['screenshot']['error'] != 4) {

    $screenshot_file = $_FILES['screenshot'];

    if(is_uploaded_file($screenshot_file['tmp_name'])){

        $tokenGen = new TokenGen();
        $token = $tokenGen->genToken('',40);

        $screenshot_file_name = $page->sanitize($screenshot_file['name']);
        $screenshot_file_name = basename($screenshot_file_name);
        $file_extension = explode('.', $screenshot_file_name);
        $file_extension = strtolower(end($file_extension));
        $screenshot_file_name = $uid.'_'.$token.'.'.$file_extension;
        $file_size = $screenshot_file['size'];
        $file_tmp = $screenshot_file['tmp_name'];
        $file_error = $screenshot_file['error'];

        $allowed_file_extensions = 'jpg, jpeg, png, gif';

        if (strpos($allowed_file_extensions,$file_extension) === false) {
            $errors[] = 1;
        }

        if ($file_size > 3000000) {
            $errors[] = 1;
        }

        if (!$file_error == 0) {
            $errors[] = 1;
        }

0 个答案:

没有答案