首先,这是我的代码:
private Shoe ProcessForm(Shoe shoe, HttpPostedFileBase image)
{
try
{
shoe.Slug = CMSHelper.SanitizeTitle(shoe.Name);
shoe.LastModification = DateTime.Now;
if ((image != null) && (image.ContentLength > 0))
{
string fileName = String.Concat(shoe.ShoeId, Path.GetExtension(image.FileName));
shoe.Image = fileName;
string filePath = Path.Combine(Server.MapPath(shoe.ImagePath), fileName);
image.SaveAs(filePath);
}
}
catch (Exception e)
{
throw e;
}
return shoe;
}
在本地,此代码运行良好。目录的权限很好。而且它之前曾在其他服务器上随机运行过(当我测试 VPS 提供商时,我在 4 或 5 个不同的服务器上测试了此代码)。
但是,如果我尝试从家用计算机运行它,一切都会顺利,数据库中保存了一个文件名,但没有上传文件。并且没有例外!!!
我已经尝试解决这个问题近三天了,花了很多无用的时间,请帮助我......我只是不明白这有什么问题......
我最终做了一个解决方法,效果非常好。我什至问过我的工作,每个人都说没有任何问题。所以搞砸了,这就是我所做的:
我没有调用 .SaveAs() 而是创建了一个方法:
public static void WriteFileFromStream(Stream stream, string toFile)
{
using (FileStream fileToSave = new FileStream(toFile, FileMode.Create))
{
stream.CopyTo(fileToSave);
}
}
我这样称呼它:
CMSHelper.WriteFileFromStream(image.InputStream, filePath);
就是这样。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)