2
I wonder if it would be possible to turn videos and audios into Base64, I already posted a question related to transforming image into Base64 and you helped me, but now I need to do the same for audio and video, I had thought it would be the same image process but it’s not working.
This is the link from my previous question and the solution I also posted here
The application I’m developing is mobile, I need to convert to send to the server and then the server disconnect and work with the file.
This is the code of my server side that receives as parameter the file in Base64 and the name to convert and save in a directory:
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
System.IO.Stream body = context.Request.InputStream;
System.Text.Encoding encoding = context.Request.ContentEncoding;
System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding);
//obtem postDATA
string postData = reader.ReadToEnd();
NameValueCollection param = HttpUtility.ParseQueryString(postData);
//params
// filename = nome do arquivo
// d = conteudo binario codificado em base64
// UserId = id do usuario
// Date = data no formato json
//salva arquivo
if (param["filename"] != null && param["d"] != null)
{
Directory.CreateDirectory("C:\\Uploads");
string strSaveLocation = ("C:\\Uploads") + "\\" + param["filename"];
FileStream fs1 = new FileStream(strSaveLocation, FileMode.OpenOrCreate, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs1);
try
{
//tenta converter vindo em base64
byte[] decbuff = Convert.FromBase64String(param["d"]);
bw.Write(decbuff);
}
catch (Exception e)
{
//failover - salva sem conversao
bw.Write(param["d"]);
}
bw.Close();
fs1.Close();
//responde OK
context.Response.Write("{\"StatusRetorno\":0,\"MensagemRetorno\":\"Operacao_Realizada\",\"idSync\":}");
}
else
{
//responde erro
context.Response.Write("{\"StatusRetorno\":1,\"MensagemRetorno\":\"could not save file\"}");
}
body.Close();
reader.Close();
}
Thanks, however I have a doubt, the return of the converted file comes by the variable
base64
, if the file is an image or video and I want to take the value inbase64
and display below the textarea(to test whether the conversion is actually working correctly) how should I do? I tried sending sodocument.getElementById("imgBase64").innerHTML = '<img src="data:image/png;base64,' + base64 + '">';
but it didn’t work.– Vinicius Vaz
Vinicius, once you have a blob, you can create a url for the blob, I will update the example.
– Tobias Mesquita
Vinicius, I updated the example to mount the image in the source itself, however I left an excerpt of commented code, where we create a URL for the file in memory (which is what I recommend).
– Tobias Mesquita
it worked, however with my previous code I sent the file in Base64 to an ajax and it sent to my server side that decoded and saved the file in a directory, now it is not able to decode, it sends the file to my directory but the image for example opens all black, I will post the code of my server side if you can help me. (Thank you for your attention so far)
– Vinicius Vaz
Vinicius, there was a mistake in
blobToBase64
, try again now, remember that you also have to pass the type of the object serialized in AJAX.– Tobias Mesquita
is working now, the server side is decoding, I will pass the image as soon as the user takes the photo, he will not be able to take from the gallery, I have as return of the photo taking function the path+name+extension (file:/Storage/../photo.jpg), has how I pass this return to the encoding function and she perform the encoding?
– Vinicius Vaz
Let’s go continue this discussion in chat.
– Vinicius Vaz
tried to use the camera.getPicture, it even returns you the image in the format you need.
– Tobias Mesquita
would it be possible to chat? It’s easier for me to explain my situation
– Vinicius Vaz
firewall does not allow.
– Tobias Mesquita
OK @Tobymosque, what happens is that the user can send multiple media (photo, audio, video) until the moment my app is taking these files and saving all their paths in a div, which then turn into an array and send to my ajax upload data, I have a function that works for images, what I needed to know is if I would have how to adapt the function that sent me to receive the media path transform it and send to my ajax. (The camera.getPicture does not provide why not send the moment I take the photo I have to send later, when the user wants to register)
– Vinicius Vaz
@Viniciusvaz, in this case you can use a Storage, create a short time table
CREATE TABLE IF NOT EXISTS FILES (file_uuid unique, name, type, base64)
to store the files, so when the user needs to upload it would only have to read the Storage and send by AJAX.– Tobias Mesquita