I have a problem when I POST a JPEG file with WebClient.
I have converted my "image.jpg" to byte [], I send it to the Web Service but the image is corrupted.
There is a specific way to convert a JPEG file to byte []?
Thanks for your help.
There is no specific way. Files are just files to the system it does not care if it is a image, video or a dll.
To read a file into a byte stream you can use:
File.ReadAllBytes(fileName);
I personally would use EasyHTTP (https://github.com/hhariri/EasyHttp) to post the file stream:
HttpClient client = new HttpClient();
client.Post(URL, ByteStream, ContentType);
Related
I've been trying to manipulate a file that's base64 encoded that I'm recieving from my client.
I'm currently using https://github.com/zdavatz/spreadsheet/blob/master/GUIDE.md to manipulate it, however, there doesn't appear to be any way to open a file directly from the base64 blob, or should I write it and then read from it? can't that a potential security threat for the server?
for example, if I recieve a file :
file = params[:file] with contents:
data:application/vnd.ms-excel;base64,0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAOwADAP7
(should I remove the data:application/vnd.ms-excel;base64, ?)
I'd like to open it with this:
Spreadsheet.client_encoding = 'UTF-8'
book = Spreadsheet.open "#{Rails.root}/app/assets/spreadsheet/event.xls"
(or with a blob or temp fle)
Sorry if it's pretty obvious, been looking for hours and there's not much info about it available, tried creating a temp file first but I don't think that's supported and there's not much I can get from the docs.
Shot in the dark: Maybe decode it, write to binary-enabled tempfile, and then feed that to Spreadsheet?
tmpfile = Tempfile.new.binmode
tmpfile << Base64.decode64(params[:file])
tmpfile.rewind
book = Spreadsheet.open(tmpfile)
There is a Web Service that is running in an external system. The Web Service expects an XML file which is Base64 encoded.
In my ABAP program, I have the XML that I want to post to the Web Service in a XSTRING variable.
The issue here is that the Base64 version of a string and a file which contains the the same string is inherently different.
The most direct solution that I know is to write the string to a file and then convert the file into Base64 and post it to the Web Service. The problem here is that the XML string that I have to too sensitive to be stored in the file system even temporarily.
So my question:
Is there a way to create a file during runtime in memory, add the XML that I have into that file in memory and finally convert it to a Base64 string, all during the runtime without ever using the actual file system.
DATA: lv_xstring TYPE xstring.
xstring = '<document>some xml data</document>'.
xstring --> into a file in memory --> convert to a Base64 string
You can directly convert string or xstring to base64 in ABAP. Check SCMS_BASE64_ENCODE_STR FM for xtsring to base64 string conversition.
On the other hand, generally binary data (xstring) transport as base64 encoded string in SOAP body. So may be you don't need to convert it to base64, just convert xml to xstring then assign it to method parameter.
I have a file stream generated by .xlsm file as savon response ruby. How do I decode or convert the stream and save it to file_name.xlsm?
#body=
"http://schemas.xmlsoap.org/soap/envelope/\">http://tempuri.org/\">212706.0_PCT.xlsmhttp://tempuri.org/\">UEsDBBQABgAIAFs+Lk1un6t7iQIAAMkWAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...........
Use the Nokogiri gem to extract the content from the element in #body. Then use Base64.decode64() to convert the data into binary and finally write it to a file.
I'm trying to figure out how to convert an image from a stream with ImageResizer (http://imageresizing.net/).
I have tried something like this.
Stream s = WebRequest.Create("http://example.com/resources/gfx/unnamed.webp").GetResponse().GetResponseStream();
ImageBuilder.Current.Build(s, "~/resources/gfx/photo3.png", new ResizeSettings("format=png"));
But i just get the error
"File may be corrupted, empty, or may contain a PNG image with a single dimension greater than 65,535 pixels."
When i do
using (Stream output = File.OpenWrite(Server.MapPath("~/resources/gfx/test.webp")))
using (Stream input = WebRequest.Create("http:///example.com/resources/gfx/unnamed.webp").GetResponse().GetResponseStream()) {
input.CopyTo(output);
}
ImageBuilder.Current.Build("~/resources/gfx/test.webp", "~/resources/gfx/photo3.png",
new ResizeSettings("format=png"));
It works fine, am i'm missing something here?
It's possible that 'output' has not been flushed to disk. .NET 4+ doesn't guarantee the file's actually written to disk just because you disposed the stream.
I assume you have the ImageResizer.Plugins.WebP plugin installed?
I am currently struggling to upload multiple files from the local storage to the Azure Blob Storage, I was wondering if anyone could help me, below is the code i was previously using to upload a single zip file.
private void SaveZip(string id, string fileName, string contentType, byte[] data)
{
// Create a blob in container and upload image bytes to it
var blob = this.GetContainer().GetBlobReference(fileName);
blob.Properties.ContentType = contentType;
// Create some metadata for this image
var metadata = new NameValueCollection();
metadata["Id"] = id;
metadata["Filename"] = fileName;
}
SaveZip(
Guid.NewGuid().ToString(),
zipFile.FileName,
zipFile.PostedFile.ContentType,
zipFile.FileBytes);
Thanks, Sami.
It's quite straightforward with Set-AzureStorageBlobContent from azure storage powershell.
ls -File -Recurse | Set-AzureStorageBlobContent -Container upload
MSDN documentation : http://msdn.microsoft.com/en-us/library/dn408487.aspx
I don't think there's any build-in methods you can use to upload multiple files to the BLOB. What you can do is to upload them one by one, or parallel.
If you're just starting to work with Blob Storage, I'd encourage you to take a look at the "How to" article we've published. Specifically, the section on "How to Upload a Blob into a Container" should be helpful. Beyond that, Shaun is correct - there is no built-in support in the StorageClient library for uploading multiple files at once, but you can certainly upload them one-by-one.
If your need is just to get it done, and not to make an app out of it, you should consider checking out Cloud Storage Studio.
Like CodeThug said, "You never do anything with the byte array".
You have to upload the data stream to the blob and you are done.