简体   繁体   中英

matching first occurrence of a character

I have a Base64 string that can start with data:image/png;base64 or any other format like data:video/mp4 based on the file uploaded by the user, i am writing an ajax call for a function that should take that base64 string and fetch it's type and something later

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RD.....

so if all the base64 string should start with : data:type/type;base64, i want to find the first occurence of , and then save the data:type/type;base64 in a string to know the type and make my original base64 string look like this : data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RD....

what i did is this :

 $.ajax({
        type: 'POST',
        url: "Uploadfile.aspx/uploadfile",
        data: '{ "fileData" : "' + data+ '" }',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (msg) {
            alert('Image saved successfully !');
        }
    });

where data is the whole base64 string

c#

 [WebMethod(EnableSession = true)]
public static void uploadfile(string fileData)
{
  Regex r = new Regex(/[^;]*/);
  Match m = r.Match(fileData);
   while (m.Success)
   {
      // how can i continue my function 
   }

}

is my logic correct or there is a better way to do that ,and how can i continue my function can anyone help . i am stuck and i don't know how to continue

You could try something like this:

Regex r = new Regex("^data:(.*?);base64,(.*?)$");
Match m = r.Match(fileData);
if(m.Success) {
    string mimeType = m.Groups[1].Value;
}

Which matches the pattern:

data: MimeType ;base64, FileData

Meaning m.Groups[1] will contain the MIME Type and m.Groups[2] will contain the base64-encoded string data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM