I have c# client and c# server (mvc4 api controller) and I want to transfer image via json.
I write on the client:
var memoryStream = new MemoryStream();
image.Save(memoryStream, ImageFormat.Jpeg);
var baseStr64 = Convert.ToBase64String(memoryStream.ToArray());
response.Image = baseStr64;
(new JavaScriptSerializer).Serialize(response);
...Sending
Without image controller gets the request class normal, but with image base64 string field I have a null parameter in controller. Then I noticed that output json fails in online json validators on this base64 field.
My output json can be found here: http://pastebin.com/wnAJpZGV
How to transmit image correctly?
It is possible that the Json string is too big. The PasteBin example you showed was over 200K in length. Check this StackOverflow article on how you might be able to fix the problem:
Can I set an unlimited length for maxJsonLength in web.config?
Related
I am new to Web APIs, so sorry if I am missing information. I am trying to send an email from my web api with an image (not as an attachment), but I am having issues with the inline html.
Example (This is in the WebAPI Controller)
message Body = "<p>Here is an image</p>" + Model.Image;
Now this will not work, as it displays "System.Byte[]", so my alternative was to convert it with some inline html, such as...
message Body = "<p>Here is an image</p>" + "<img src=\"data:image/gif;base64,#System.Convert.ToBase64String{Model.Image)\" />;
This will return an empty element. What am I doing wrong? If I were to guess, I am not using quotations correctly, but I cannot find anyone trying to insert an image the way I am.
Site note - If I see what is being sent from the mobile app for the image, I am getting a huge string of text that is always the same, regardless of whether it is a different image. I thought I would use that like src="data:image/gif;base64,", but I just get a long string returned in the email.
You need to host your image somewhere else and use the full path. Example:
<img scr="https://static.makeuseof.com/wp-content/uploads/2017/12/test-html-online-670x335.jpg" />
Most email clients do not display base64 images. My solution was to attach the image to the email by using the following code.
MemoryStream ms = new MemoryStream(Model.Image);
message.Attachments.Add(new Attachment(ms, "Image.png", "image/png"));
I did not need to convert from byte to base64, as this was done on the mobile app before being sent to the API, so I just referenced the value that was stored in the model.
I have inherited a C# tool that sends a serialized byte array of meeting data to an online calendar application. The online calendar uses a node.js API. My problem is that the serialized byte array is not getting parsed correclty by node.js (IE, in express the req.body object is empty). I am look for either a way to send the data so it can be parsed by Node.js, or a parser in Node.js that can handle c# byte arrays. It is JSON formatted. Here is the code that sends the request:
string json = JsonConvert.SerializeObject(reallyPost, Formatting.None);
byte[] postThisz = Encoding.ASCII.GetBytes(json);
byte[] response = wc.UploadData(siteUrl, postThisz);
At the moment I'm simply trying to log the data. The route is caught here:
app.post('/remotePost', api.remotePost);
which uses this controller:
module.exports.remotePost = function (req, res) {
console.log(req);
console.log(req.body);
sendJsonResponse(res, 200, "remote posted.");
};
The req.body gets populated by my parsing middleware:
app.use(bodyParser.json());
However this cannot handle the byte array sent by the C# program. I can solve this either by finding a parser that will handle the C# POST correctly (byte[] type), or finding a C# module that can make a post containing something of the format that JSON.stringify() returns.
It's advisable to change the encoding to utf8
Encoding.UTF8.GetBytes(json);
Then, you should also set the appropriate headers before upload
wc.Headers.Add("Content-Type", "application/json; charset=utf8");
I am using the Google Charts API in my .NET application and I would like to programmatically save the chart rendered as an image by passing the chat URI in an AJAX request to the server.
The problem is that the URI of the chart is a data URI scheme so it looks like this: data:image/png;base64,abcdef...
I tried to request this URI with the HttpClient, WebClient and WebRequest classes but all of these launched an exception saying that the URI prefix is not recognized or not handled...
Does anyone knows wich library I can use to request this kind of URI?
By exploring the data URI scheme by myself, I finally understood that I didn't need to send a request to retrieve the image, as the bytes were already in the URI.
I managed to decode the base 64 string to an 8 bit stream and create the image :)
Below is a short example of my working code :)
// The data URI to the image
String imageLink = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAEsCAYAAADtt+XCAAAgAElEQVR4Xu2dD3RU1bX/976T0UQESdEfPluVJEj981u0ZILWdi2LzgQe7XtFV+k/adUqmRRUwD/4lEq1Vq3aWuu/WqBWW21NsKKVp7QwU6EVa8wfsMWnlRLwgSCFQpRKIJm5+609zGCIA8k5ZDJ3br6zlku93H3uOZ99Zj6cc889lwkfEAABEAABELAgwBYxCAEBEAABEAABgkDQCUAABEAABKwIQCBW2BAEAiAAAiAAgaAPgAAIgAAIWBGAQKywIQgEQAAEQAACQR8AARAAARCwIgCBWGFDEAiAAAiAAASCPgACIAACIGBFAAKxwoYgEAABEA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JQCD+zS1aBgIgAAI5JQCB5BQvCgcBEAAB/xKAQPybW7QMBEAABHJKAALJKV4UDgIgAAL+JQCB+De3aBkIgAAI5JQABJJTvCgcBEAABPxLAALxb27RMhAAARDIKQEIJKd4UTgIgAAI+JcABOLf3KJlIAACIJBTAhBITvGicBAAARDwLwEIxL+5RctAAARAIKcEIJCc4kXhIAACIOBfAhCIf3OLloEACIBATglAIDnFi8JBAARAwL8EIBD/5hYtAwEQAIGcEoBAcooXhYMACICAfwlAIP7NLVoGAiAAAjklAIHkFC8KBwEQAAH/EoBA/JtbtAwEQAAEckoAAskpXhQOAiAAAv4lAIH4N7doGQiAAAjklMD/AXVzTIZhC7uhAAAAAElFTkSuQmCC"
// Split the string to escape the real data
var b64 = imageLink.Split(",".ToCharArray(), 2);
// Convert the base 64 String to byte array
byte[] byteArray = Convert.FromBase64String(b64[1]);
// Open a stream from the byte array
MemoryStream imgStream = new MemoryStream(byteArray);
// Generate the image from the stream
Image img = Image.FromStream(imgStream);
// Save the image
img.save("C:\\Images\\image.png");
I want to send a normal text file from my VisualStudio C# Windows Forms Application via "Post" to an Url with a WebClient.
my code:
using(WebClient w = new WebClient())
{
w.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string HtmlResult = w.UploadString(#"http://xxx/api/test", "Post", #"C:\Temp\T.txt");
}
This is what I found so far. When I run this code the post method in my web project gets hit but the param is null (which is understandable since its data type is a string but it is also null when I change the file address to a simple string like "test").
So my real problem is that I canĀ“t send my text data to my URL.
First, your ContentType should be "application/text" not "application/x-www-form-urlencoded".
Second, this method defaults to POST for HTTP/HTTPS so use the overload that takes two string parameters UploadString("destination Url", "data"). If you just must use the three parameter overload, the method should be "POST" not "Post".
This method does not read the file for you, you will need to add code to read the contents of your file into a string, then pass that string to the data parameter of the UploadString method.
I probably should add that, in the recieving api you will read the data from the Request.InputStream object. As with any stream you will read it into a byte array and need to encode it back to a string. You must use the same encoding that was used to read the file into a string.
The stream InputStream is a one-way readonly stream, so you will need to read it in it's entirety into a byte array before encoding back to a string.
I am writing a console application that needs to perform a POST to an MVC controller. I am using the WebClient class to perform the POST. But I'm having trouble understanding how to add arrays to the message body.
For simple parameters, it seems to work if I do this:
using (var client = new WebClient())
{
var values = new NameValueCollection
{
{ "userName", "userName" },
{ "password", "passwordGoesHere"}
};
byte[] responseArray = client.UploadValues(String.Format("{0}/Mobile/StartSession", serverAddress), values);
Debug.WriteLine(String.Format("\r\nResponse received was :\n{0}\n", Encoding.ASCII.GetString(responseArray)));
}
I was trying to find how to pass arrays in the message body when using WebClient (for calling one of the other methods). I came across this solution: POST'ing arrays in WebClient (C#/.net)
It appears the solution actually passes parameters in the query string (and not in the message body). This seems to work in any case, as the HttpPost method on the MVC controller is still receiving the correct information. However, another method requires that I pass an image as an array of bytes. This is too large to be passed in the querystring and so the call fails.
So my question is, using the code I provided above, how can I add arrays in there as well. So an array of bytes for example, but also an array of strings.
If any one can provide me with a solution it would be much appreciated, or if I'm incorrect in my thinking please let me know.
Thanks
Instead of using array of bytes maybe you should POST a file in the same way files are uploaded from browser from file inputs. This way you will save some transfered bytes, but you have to use HttpWebRequest instead of WebClient. More about this solution is here:
Upload files with HTTPWebrequest (multipart/form-data)
You upload bytes as "multipart/form-data" content type. On the server you will receive the streams of bytes in Request.Files collection.