Skip to content

Instantly share code, notes, and snippets.

@cmcdonaldca
Created September 25, 2012 16:57
Show Gist options
  • Save cmcdonaldca/3783134 to your computer and use it in GitHub Desktop.
Save cmcdonaldca/3783134 to your computer and use it in GitHub Desktop.
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
string fileName = System.IO.Path.GetFileName(file.FileName);
string path = System.IO.Path.Combine(Server.MapPath("~/Images"), fileName );
if (IMAGE_FILE_TYPES.Contains(Path.GetExtension(file.FileName).ToUpperInvariant()) && file.ContentLength <= MAX_FILE_SIZE)
{
file.SaveAs(path);
string base64Image = Convert.ToBase64String(System.IO.File.ReadAllBytes(path));
// From the shopify docs, this is the expected format
// (http://api.shopify.com/asset.html#update)
//{
// "asset": {
// "key": "assets/empty.gif",
// "attachment": "R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==\n"
// }
//}
//so create the .NET equivalent of that
dynamic assetData = new
{
asset = new {
key = String.Format("assets/{0}", fileName),
attachment = base64Image
}
};
var api = (ShopifyAPIClient)Session["API"];
dynamic createBlogResponse = api.Put("/admin/themes/3175624/assets.json", assetData );
// check response for valid call and delete image locally or put out error message if an error occurred
System.IO.File.Delete(path);
ViewBag.Message = "File uploaded successfully";
}
return View();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment