将图像发布到php脚本 [英] Posting image to php script

查看:67
本文介绍了将图像发布到php脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





有人可以帮我解决以下问题吗?



如果我有在磁盘上的jpg图像,很容易将其发布到php脚本并将其保存在服务器端,如下所示:



C#

Hi,

Could anyone please help me with the following please?

If I have a jpg image on disk, it is easy to post it to a php script and save it server-side as follows:

C#

System.Net.WebClient Client = new System.Net.WebClient();

Client.Headers.Add("Content-Type", "binary/octet-stream");

byte[] result = Client.UploadFile("http://mywebsite.com/UploadImage.php", "POST", "PicData.jpg");

szResult = System.Text.Encoding.UTF8.GetString(result, 0, result.Length);



和PHP如下:



PHP




and the PHP is as follows:

PHP

<?php

$uploaddir = "uploads/";

$szResult = "";

if (is_uploaded_file($_FILES["file"]["tmp_name"]))
{
    $uploadfile = $uploaddir.basename($_FILES["file"]["name"]);

    if (move_uploaded_file($_FILES["file"]["tmp_name"], $uploadfile))
    {
        $szResult = "Uuploaded successfully.";
    }
    else
    {
        $szResult = "Move to upload folder failed.";
    }
}

else
{
    $szResult = "Upload failed.";
}

echo $szResult;

?>





这一切效果很好。



但是,如果我将jpg文件作为 byte [] 调用 jpegAsByteArray



那么我能做到:



This all works great.

But, what if I have the jpg file in memory as a byte[] called jpegAsByteArray.

Well then I could do:

File.WriteAllBytes("PicData.jpg", jpegAsByteArray);



然后调用上面的C#代码,一切都会正常工作。



但它接缝浪费将它保存到磁盘然后发送使用UploadFile到服务器的字节。



如何在服务器上实现文件的保存没有也将其保存在客户端上一边?



谢谢,



Mitch。



已添加代码块[/ Edit]


And call the above C# code and all will work fine.

But it seams wasteful to save it out to disk just to then send the bytes to the server using UploadFile.

How can I acheive the saving of the file on the server WITHOUT also saving it on the client side?

Thanks,

Mitch.

Code block added[/Edit]

推荐答案

uploaddir = uploa DS /;

uploaddir = "uploads/";


szResult = ;

if (is_uploaded_file(
szResult = ""; if (is_uploaded_file(


_FILES [ file] [ tmp_name]))
{
_FILES["file"]["tmp_name"])) {


这篇关于将图像发布到php脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆