Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I need to conver the image file to string or variable in javascript. This is need for html 5.
Posted
Comments
Rajesh Anuhya 24-Jan-12 6:09am    
Question not clear.
janablisslogix 24-Jan-12 6:21am    
I have one file upload control in html,after select the image file its need to convert to Base64 string. After converting i will store the string to server

1 solution

if you want to upload file and save as string on server then you should use upload file and use stream as byte array and convert it to base64 string

you can use following code

ASP.NET
<asp:fileupload id="flupUpload" runat="Server"   />
       <asp:button id="Button1" runat="server" onclick="Button1_Click" text="Button"   />



C#
protected void Button1_Click(object sender, EventArgs e)
{

    int iLen = flupUpload.PostedFile.ContentLength;
    byte[] btArr = new byte[iLen];
    flupUpload.PostedFile.InputStream.Read(btArr, 0, iLen);
    Response.Write(Convert.ToBase64String(btArr));
    //      Convert.ToBase64String(btArr) -: this is your base64 string
}




mark as solution if you problem solved
 
Share this answer
 
v2
Comments
janablisslogix 24-Jan-12 7:59am    
thanks for your replay

But i need the java script code to convert Base64 from image, and you must consider i have the local path of image
Technoses 24-Jan-12 10:42am    
why u want to use js and not cs
as per above comment you want to store string to server then you can us this process. tell us what actually you want to do?
janablisslogix 25-Jan-12 0:24am    
thanks for your reply.
I am doing HTML 5 project using sencha touch. So i need to store the image to server as a base64 string only. I put one file upload control,and convert the image file to base64, and then store it into the server.To do this i can not able to get the image from local(client) file path using javascript.
Actually now i can able to get the base64 string from image and this is working only if the image kept from the project folder. Biut i need to select the image from client machint.
Note:
I hardcode this one "file:///C:\Users\Administrator\Desktop\Ponkal\images.jpg" to set the image from local machine but it is not working. i got the path from file upload control as C:\fakepath\images.jpg. I think the fake path is the problem for me but i am not sure. How can i eliminate the fake path in google
chrome.
Technoses 25-Jan-12 8:28am    
if you want to select the image from client machine and store as string then the above process is useful for that. File upload contorl does not work with ajax also so you should upload file first from client to server and then you can able to convert as string from image..

or i cant able to understand what do you want to do..
Technoses 25-Jan-12 1:58am    
if you want to select the image from client machine and store as string then the above process is useful for that. File upload contorl does not work with ajax also so you should upload file first from client to server and then you can able to convert as string from image..

or i cant able to understand what do you want to do..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900