Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want when insert a new base64 in web service it is create image002.jpg in my folder path because image001.jpg already in folder path. Could you help how to fix it ?


What I have tried:

This is my coding in webservice

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

public void Base64ToImage(string imagestr)
    {

        DbAccessConnection conn = getActiveConnection();
        try
        {
            beginTransaction(conn);

            byte[] bytes = Convert.FromBase64String(imagestr);

            using (MemoryStream ms = new MemoryStream(bytes))
            {
                Image pic = Image.FromStream(ms);
                pic.Save("C:/image/image001.jpg");
            }

            commitTransaction(conn);
            Responder.writeResponse(true, "Sucess Convert Base64 String To Image");
        }
        catch (Exception ex)
        {
            rollbackTransaction(conn);
            Responder.writeResponse(false, ex.Message);
        }
    }
Posted
Updated 29-Dec-19 20:00pm

Try this:
static int picCount = 1;

public void Base64ToImage(string imagestr)
    {

        DbAccessConnection conn = getActiveConnection();
        try
        {
            beginTransaction(conn);

            byte[] bytes = Convert.FromBase64String(imagestr);

            using (MemoryStream ms = new MemoryStream(bytes))
            {
                Image pic = Image.FromStream(ms);
                pic.Save("C:/image/image" + picCount.ToString("D3") + ".jpg");
                picCount++;
            }

            commitTransaction(conn);
            Responder.writeResponse(true, "Sucess Convert Base64 String To Image");
        }
        catch (Exception ex)
        {
            rollbackTransaction(conn);
            Responder.writeResponse(false, ex.Message);
        }
    }

If you restart your webservice, counting will start from 1 again, so if you want to continue from the last number you will have to save it (e.g. as a setting).
 
Share this answer
 
v2
Comments
Reyhan M.T 30-Dec-19 2:32am    
It is code is working, Thank you so much
RickZeeland 30-Dec-19 2:47am    
A good start of the new year :)
Richard MacCutchan 30-Dec-19 8:28am    
It's still 2019 in Surrey.
RickZeeland 30-Dec-19 10:17am    
Oops, rounding error !
Read all the files currently in the folder: Directory.GetFiles[^] will do that for you, and you can provide wildcards like "image*.jpg" to restrict them.
Then you could just sort them by path - but with only three digits, you may start to have problems quite quickly as string based comparisons work on the first non-matching character, so once you exceed three digits the ordering will fail.

A better approach is to use a custom comparer and return the file name with the max count, by breaking the file name down and converting the number portion to an integer.

Once you have the "highest value" currently on disk, you can add one to it and save the file as the new name, making sure to handle any "file exists" errors and repeat the whole operation if that happens (because web activities are "naturally" multiuser, and a different user may have used that number already).

But that's messy.
The way I'd do it is to store the file under a GUID filename, and use a Database to maintain a list of file name vs an Identity column to store the index number. Sounds harder, but it's trivial to do in practice, and a lot quicker to execute as well - particularly when you start to get a lot of files involved.
 
Share this answer
 

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