开发者

Unable to Pass Image in HTTP POST in a HTTP Web Request in ASP.NET

I am having trouble converting an image into bytes and saving it in database.

Here is the description,

I have an image that will be send from a remote device to the web server using HTTP POST.

so what I am doing is I ask them to sent the image to me.

Since the data is sent in POST i assume they will send me the Bytes by converting the bytes into string using

byte[] img = FileUpload1.FileBytes; Encoding enc = Encoding.ASCII; string img = enc.GetString(img);

Then they make a WebRequest using HTTPWebRequest and append this image in HTTP POST.

The Whole Code for making the request ---

    WebRequest request = WebRequest.Create(url);
    request.Method = "POST";


    //Create the POST Data

    FileUpload img = (FileUpload)imgUpload;
    Byte[] imgByte = null;
    if (img.HasFile && img.PostedFile != null)
    {
        imgByte = imgUpload.FileBytes;
    }
    string imgPh = null;
    Encoding enc = Encoding.ASCII;
    if (imgByte != null)
    {
        imgPh = enc.GetString(imgByte);
    }
    string postData = "sid=8062BD53EB4552AD6D0FBB7E5DC5B7AF&status=Y&uid=123456789012&fname=Dinesh Singh&lname=Malik&ftname=Balwan&yrbirth=1988&gender=Male&开发者_如何学运维;address1=Address1&address2=Address2&address3=Address3&address4=Address4&imagePh=" + imgPh;


    byte[] post = Encoding.UTF8.GetBytes(postData);


    //Set the Content Type
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = post.Length;

    Stream reqdataStream = request.GetRequestStream();
    // Write the data to the request stream.
    reqdataStream.Write(post, 0, post.Length);

    reqdataStream.Close();

    // If required by the server, set the credentials.
    request.Credentials = CredentialCache.DefaultCredentials;
    WebResponse response = null;
    try
    {
        // Get the response.
        response = request.GetResponse();

    }
    catch (Exception ex)
    {
        Response.Write("Error Occured.");
    }

On the Page to which Request is made I am getting this image again into bytes using

    Encoding enc = Encoding.ASCII;

    byte[] imagePhoto = enc.GetBytes(postData["imageph"]);

From Here I save it into my Database

But when I retrieve the image using the Handler, it does not show the image.


The issue is the conversion of the image from byte[] to string and then converting string into byte[] at the Web Server. (Because when I save the image directly without this conversion using TestPage on server it shows the image.)

So What am I doing wrong in this.

Also is there any way in the above code to get the data of HTTP Post as received by the Web Server (retrieve HTTP Headers). I want to retrieve this data received to sent it back to the Other Development to develop the request at the device in the same format as I am receiving in the HTTP Web Request URL

Any help would be appreciated.


Ok I think finally I figured it out.

What I did was -

Image -> byte[] -> Convert.ToBase64String -> Append The String Data to Post Request

At the Server-

Get the Post Data -> Convert.FromBase64String -> byte[] -> Insert into Database

Works Great...:)

Thanks

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜