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

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

问题描述:

我无法将图像转换为字节并将其保存在数据库中。无法在ASP.NET中的HTTP Web请求中的HTTP POST中传递图像

下面是本说明书中,

我有将从远程设备被发送到使用HTTP POST web服务器的图像。

所以我正在做的是我请他们把图像发给我。

由于数据是在POST i发送假设它们将被转换成字节串使用的字节发送我

字节[] IMG = FileUpload1.FileBytes; 编码enc = Encoding.ASCII; string 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"]); 

从这里再一次得到这个形象成字节我救它到我的数据库

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


的问题是从字节[]到字符串,然后将字符串转换成字节[]在Web服务器的图像的转换。 (因为当我在服务器上使用TestPage时没有使用TestPage直接保存图像时,它会显示图像。)

所以我在做什么错了。

在上面的代码中还有什么方法来获取Web服务器接收到的HTTP Post数据(检索HTTP头)。 我想取出这个数据接收发回给其他开发研制的设备在相同的格式要求,因为我在HTTP的Web请求的URL

任何帮助,我接受,将不胜感激。

好吧我想我终于明白了。

我做什么 -

图片 - >字节[] - > Convert.ToBase64String - >追加字符串数据POST请求

在全服务器

得到这个职位数据 - > Convert.FromBase64String - >字节[] - >插入到数据库

的伟大工程... :)

感谢