无法在 ASP.NET 中的 HTTP Web 请求中通过 HTTP POST 传递图像

发布于 2024-11-18 12:59:31 字数 2311 浏览 3 评论 0原文

我在将图像转换为字节并将其保存在数据库中时遇到问题。

这是描述,

我有一个图像将使用 HTTP POST 从远程设备发送到 Web 服务器。

所以我要做的就是要求他们将图像发送给我。

由于数据是在 POST 中发送的,我假设他们会通过使用

byte[] img = FileUpload1.FileBytes; 将字节转换为字符串来向我发送字节。 编码 enc = Encoding.ASCII; 字符串 img = enc.GetString(img);

然后,他们使用 HTTPWebRequest 发出 WebRequest 并将该图像附加到 HTTP POST 中。

发出请求的整个代码 ---

    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.");
    }

在发出请求的页面上,我使用

    Encoding enc = Encoding.ASCII;

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

From Here 再次将此图像转换为字节,我将其保存到数据库中

但是当我使用处理程序检索图像时,它不显示图像。


问题在于将图像从 byte[] 转换为字符串,然后在 Web 服务器上将字符串转换为 byte[]。 (因为当我直接保存图像而不使用服务器上的 TestPage 进行此转换时,它会显示图像。)

那么我在这方面做错了什么。

上述代码中是否有任何方法可以获取 Web 服务器接收到的 HTTP Post 数据(检索 HTTP 标头)。 我想检索收到的数据,并将其发送回其他开发,以便以与我在 HTTP Web 请求 URL 中收到的格式相同的格式在设备上开发请求

。如有任何帮助,我们将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栀子花开つ 2024-11-25 12:59:31

好吧,我想我终于明白了。

我所做的是 -

图片 ->字节[] -> Convert.ToBase64String ->将字符串数据附加到

服务器的

Post 请求- 获取 Post 数据 -> Convert.FromBase64String ->字节[] ->插入数据库

效果很好...:)

谢谢

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文