大家看看这段代码有没有什么问题?图片每次上传就是失败的~~ 也没有出现任何错误提示~~希望大家可以帮帮忙
protected void upload1Button_Click(object sender, EventArgs e)
{
// proceed with uploading only if the user selected a file
if (image1FileUpload.HasFile)
{
try
{
string fileName = image1FileUpload.FileName;
string location = Server.MapPath("./ProductImages/") + fileName;
// save image to server
image1FileUpload.SaveAs(location);
// update database with new product details
ProductDetails pd = CatalogAccess.GetProductDetails(currentProductId);
CatalogAccess.UpdateProduct(currentProductId, pd.Name, pd.Description, pd.Price.ToString(), fileName, pd.Image2FileName, pd.OnDepartmentPromotion.ToString(), pd.OnCatalogPromotion.ToString());
// reload the page
Response.Redirect(Request.ApplicationPath + "/CatalogAdmin.aspx" +"?DepartmentID=" + currentDepartmentId + "&CategoryID=" + currentCategoryId +"&ProductID=" + currentProductId);
}
catch
{
statusLabel.Text = "上传图片失败";
}
}
}
protected void upload1Button_Click(object sender, EventArgs e)
{
// proceed with uploading only if the user selected a file
if (image1FileUpload.HasFile)
{
try
{
string fileName = image1FileUpload.FileName;
string location = Server.MapPath("./ProductImages/") + fileName;
// save image to server
image1FileUpload.SaveAs(location);
// update database with new product details
ProductDetails pd = CatalogAccess.GetProductDetails(currentProductId);
CatalogAccess.UpdateProduct(currentProductId, pd.Name, pd.Description, pd.Price.ToString(), fileName, pd.Image2FileName, pd.OnDepartmentPromotion.ToString(), pd.OnCatalogPromotion.ToString());
// reload the page
Response.Redirect(Request.ApplicationPath + "/CatalogAdmin.aspx" +"?DepartmentID=" + currentDepartmentId + "&CategoryID=" + currentCategoryId +"&ProductID=" + currentProductId);
}
catch
{
statusLabel.Text = "上传图片失败";
}
}
}
CatalogAccess.UpdateProduct(currentProductId, pd.Name, pd.Description, pd.Price.ToString(), fileName, pd.Image2FileName, pd.OnDepartmentPromotion.ToString(), pd.OnCatalogPromotion.ToString());
// reload the page
Response.Redirect(Request.ApplicationPath + "/CatalogAdmin.aspx" +"?DepartmentID=" + currentDepartmentId + "&CategoryID=" + currentCategoryId +"&ProductID=" + currentProductId);
这两句里有问题,或者是服务器目录没有写权限。
Message="对路径“c:\\inetpub\\wwwroot\\BalloonShop\\ProductImages\\222.jpg”的访问被拒绝。"
Source="mscorlib"
StackTrace:
在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
在 System.IO.FileStream..ctor(String path, FileMode mode)
在 System.Web.HttpPostedFile.SaveAs(String filename)
在 System.Web.UI.WebControls.FileUpload.SaveAs(String filename)
在 ProductDetailsAdmin.upload1Button_Click(Object sender, EventArgs e) 位置 c:\Inetpub\wwwroot\BalloonShop\UserControls\ProductDetailsAdmin.ascx.cs:行号 182
在 System.Web.UI.WebControls.Button.OnClick(EventArgs e)
在 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
在 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
在 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
在 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
要么你给你这个站点的目录分配 读写权限给 ASPNET账号
或者给你的这个站点绑定一个你自己建立的账号,让这个账号有权利读写这个目录。