Skip to content

Instantly share code, notes, and snippets.

@Legends
Last active October 29, 2019 14:59
Show Gist options
  • Select an option

  • Save Legends/7b1352728b052e8c8547d92582822232 to your computer and use it in GitHub Desktop.

Select an option

Save Legends/7b1352728b052e8c8547d92582822232 to your computer and use it in GitHub Desktop.
// Upload string base64 encoded image to MVC action
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<form action="http://localhost:14864/api/image" method="post" enctype="multipart/form-data">
<input type="text" name="filename" value="bla.png"/>
<input type="text" name="file" value="iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="/>
<input type="submit">Senden</input>
</form>
</body>
</html>
// C#: Uses ByteArrayModelBinder internally, which converts the base64 string to an byte[]
// POST: api/image
[HttpPost]
public void Post(byte[] file, string filename)
{
// Don't trust the file name sent by the client. Use
// Path.GetRandomFileName to generate a safe random
// file name. _targetFilePath receives a value
// from configuration (the appsettings.json file in
// the sample app).
var trustedFileName = Path.GetRandomFileName();
var filePath = Path.Combine(_targetFilePath, trustedFileName);
if (System.IO.File.Exists(filePath))
{
return;
}
System.IO.File.WriteAllBytes(filePath, file);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment