Question

I have several images in /public_html/images/ restricted using .htaccess Meanwhile I created a php file that gets an image.

Example before using POST to change specific ID

<?php>
$file = '/home/user/public_html/foodimage/ID-856-front.jpg';
header('Content-Type: image/jpeg');
print $file;
?>

How can I create my Webrequest to pull the image requested from this php file.

        Dim request As System.Net.WebRequest = System.Net.WebRequest.Create(url)
        request.Proxy = Nothing
        request.Method = "POST"
        Dim postData = postvalues
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
        request.ContentType = "application/x-www-form-urlencoded"
        request.ContentLength = byteArray.Length
        Dim dataStream As Stream = request.GetRequestStream()
        dataStream.Write(byteArray, 0, byteArray.Length)
        dataStream.Close()

        Dim response As WebResponse = request.GetResponse()
        dataStream = response.GetResponseStream()

        Dim x As New BitmapImage
        x.StreamSource() = dataStream

        dataStream.Close()
        response.Close()
        Return (x)

Please Help. I able to pull off json arrays and strings in a similar function but I can't seem to retrieve images.

Was it helpful?

Solution 2

Ok after googling for a couple of hours I was able to get it to work. Heres the code I used to be able to retrieve images from php.

php code

<?php>
$photoID = $_POST["uID"];
$file = "/home/username/public_html/imagefolder/ID-$photoID-front.jpg";
header('Content-Type: image/jpeg');
readfile($file);
exit;
?>

vb.net

    Public Function getimage(ByVal url As String, ByVal postvalues As String) As BitmapImage
    Dim request As System.Net.WebRequest = System.Net.WebRequest.Create(url)
    request.Proxy = Nothing
    request.Method = "POST"
    Dim postData = postvalues
    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
    request.ContentType = "application/x-www-form-urlencoded"
    request.ContentLength = byteArray.Length
    Dim dataStream As Stream = request.GetRequestStream()
    dataStream.Write(byteArray, 0, byteArray.Length)
    dataStream.Close()

    Dim x As New BitmapImage()
    Dim lsResponse As [String] = String.Empty
    Using lxResponse As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
        Using reader As New BinaryReader(lxResponse.GetResponseStream())
            Dim lnByte As [Byte]() = reader.ReadBytes(1 * 1024 * 1024 * 10)

            Dim stream As New MemoryStream(lnByte)
            stream.Seek(0, SeekOrigin.Begin)
            x.BeginInit()
            x.StreamSource = stream
            x.EndInit()
        End Using
    End Using

    Return x
End Function

OTHER TIPS

I'm not an expert in .NET, so I cannot help on that side, but instead of using:

print $file;

try using readfile() (http://www.php.net/readfile):

<?php>
$file = '/home/user/public_html/foodimage/ID-856-front.jpg';
header('Content-Type: image/jpeg');
readfile($file);
exit;
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top