英文:
ASP.NET MVC controller upload photo and save folder path
问题
The method I use works on other servers. It works on my computer but not on the server I just rented.
public JsonResult SaveRegesterPhoto(string mImage)
{
if (string.IsNullOrEmpty(mImage))
{
return Json("عکس نرسید", JsonRequestBehavior.AllowGet);
};
var t = mImage.Substring(22); // remove data:image/png;base64,
byte[] bytes = Convert.FromBase64String(t);
Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = Image.FromStream(ms);
}
var randomFileName = Guid.NewGuid().ToString().Substring(0, 20) + ".png";
var fullPath = Path.Combine(Server.MapPath("~/Photos/Admin/"), randomFileName);
image.Save(fullPath, System.Drawing.Imaging.ImageFormat.Png);
return Json(mImage, JsonRequestBehavior.AllowGet);
}
英文:
The method I use works on other servers. It works on my computer but not on the server I just rented.
public JsonResult SaveRegesterPhoto(string mImage)
{
if (string.IsNullOrEmpty(mImage))
{
return Json("عکس نرسید", JsonRequestBehavior.AllowGet);
};
var t = mImage.Substring(22); // remove data:image/png;base64,
byte[] bytes = Convert.FromBase64String(t);
Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = Image.FromStream(ms);
}
var randomFileName = Guid.NewGuid().ToString().Substring(0, 20) + ".png";
var fullPath = Path.Combine(Server.MapPath("~/Photos/Admin/"), randomFileName);
image.Save(fullPath, System.Drawing.Imaging.ImageFormat.Png);
return Json(mImage, JsonRequestBehavior.AllowGet);
}
答案1
得分: 2
确保托管公司没有阻止功能,并且您的应用程序有写入新服务器的权限。
我记得我以前在GoDaddy遇到过这个问题。我在开发服务器上原型制作并向客户演示的东西,一旦部署到GoDaddy的环境中,就无法正常运行。我记得花了好几个小时来弄清楚到底发生了什么,然后花了更多的时间找到一个解决方法。
如果您能提供任何错误消息的详细信息,比如浏览器开发工具的错误消息和网络选项卡详细信息,服务器生成的任何日志消息,或者您在代码中构建的日志消息等,都将有助于我们为您提供帮助。
如果没有其他信息,我可能会猜测可能是目录权限的问题。您的应用程序是否具有保存文件的权限?
英文:
Make sure the hosting company isn’t blocking functionality, and that your application has permission to write to the new server.
I recall this happening to me with GoDaddy back in the day. Stuff I would prototype, and demo to my client on my dev server, wouldn’t work once deployed to GoDaddy’s environment. Recall taking hours to figure out what the heck was happening, and taking many more hours to find a workaround.
Any error message detail you can provide will help us help you, i.e. browser dev tools error messages and network tab detail, any log messages generated at the server, or messages from logging you have built into your code, etc.
And if I had to hazard a guess without additional information, I might look at directory permissions. Does your application have permission to save the file?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论