加入收藏 | 设为首页 | 会员中心 | 我要投稿 常州站长网 (https://www.0519zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

在ASP.NET 2.0中操作数据之五十四:添加新记录时包含一个文件上

发布时间:2016-11-22 04:24:27 所属栏目:MsSql教程 来源:站长网
导读:导言: 在前面2节教程,我们探讨了如何使用FileUpload控件从客户端向服务器上传文件,以及如何在数据Web控件里显示二进制数据。 在本节,我们将创建一个web页面以添加新的种类。除了为类的name和description属性添加TextBoxes控件外,我们还要在页面上添加

  为了把上传的图片保存在新添加的记录里,我们需要在DetailsView控件的ItemInserting事件里,用上传的数据对ObjectDataSource控件的picture参数赋值。然而,在此之前,我们需要确保上传的文件为JPG而不是其它的什么格式。就象在第6步中探讨的一样,我们用文件的扩展名来检查其类型。

  虽然Categories表允许Picture列为NULL值,但所有的种类都应该有一张图片。在本页面,我们强制用户添加记录时提供图片。下面的代码确保已经上传图片,且为恰当的类型。

// Reference the FileUpload controls
FileUpload PictureUpload = (FileUpload)NewCategory.FindControl("PictureUpload");
if (PictureUpload.HasFile)
{
 // Make sure that a JPG has been uploaded
 if (string.Compare(System.IO.Path.GetExtension(PictureUpload.FileName),
  ".jpg", true) != 0 &&
 string.Compare(System.IO.Path.GetExtension(PictureUpload.FileName),
  ".jpeg", true) != 0)
 {
 UploadWarning.Text =
  "Only JPG documents may be used for a category's picture.";
 UploadWarning.Visible = true;
 e.Cancel = true;
 return;
 }
}
else
{
 // No picture uploaded!
 UploadWarning.Text =
 "You must provide a picture for the new category.";
 UploadWarning.Visible = true;
 e.Cancel = true;
 return;
}

这些代码应放在第6步中的代码前面,如果上传的文件有问题,事件处理器在文件保存到文件系统前就结束了。

假设上传的文件没有问题,然后我们用下面的代码将上传文件的数据分配给参数picture:

// Set the value of the picture parameter
e.Values["picture"] = PictureUpload.FileBytes;

完整的ItemInserting事件处理器

下面是ItemInserting事件处理器的完整代码:

protected void NewCategory_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
 // Reference the FileUpload controls
 FileUpload PictureUpload = (FileUpload)NewCategory.FindControl("PictureUpload");
 if (PictureUpload.HasFile)
 {
 // Make sure that a JPG has been uploaded
 if (string.Compare(System.IO.Path.GetExtension(PictureUpload.FileName),
  ".jpg", true) != 0 &&
  string.Compare(System.IO.Path.GetExtension(PictureUpload.FileName),
  ".jpeg", true) != 0)
 {
  UploadWarning.Text =
  "Only JPG documents may be used for a category's picture.";
  UploadWarning.Visible = true;
  e.Cancel = true;
  return;
 }
 }
 else
 {
 // No picture uploaded!
 UploadWarning.Text =
  "You must provide a picture for the new category.";
 UploadWarning.Visible = true;
 e.Cancel = true;
 return;
 }

 // Set the value of the picture parameter
 e.Values["picture"] = PictureUpload.FileBytes;
 
 
 // Reference the FileUpload controls
 FileUpload BrochureUpload =
 (FileUpload)NewCategory.FindControl("BrochureUpload");
 if (BrochureUpload.HasFile)
 {
 // Make sure that a PDF has been uploaded
 if (string.Compare(System.IO.Path.GetExtension(BrochureUpload.FileName),
  ".pdf", true) != 0)
 {
  UploadWarning.Text =
  "Only PDF documents may be used for a category's brochure.";
  UploadWarning.Visible = true;
  e.Cancel = true;
  return;
 }

 const string BrochureDirectory = "~/Brochures/";
 string brochurePath = BrochureDirectory + BrochureUpload.FileName;
 string fileNameWithoutExtension =
  System.IO.Path.GetFileNameWithoutExtension(BrochureUpload.FileName);

 int iteration = 1;

 while (System.IO.File.Exists(Server.MapPath(brochurePath)))
 {
  brochurePath = string.Concat(BrochureDirectory, fileNameWithoutExtension,
  "-", iteration, ".pdf");
  iteration++;
 }

 // Save the file to disk and set the value of the brochurePath parameter
 BrochureUpload.SaveAs(Server.MapPath(brochurePath));
 e.Values["brochurePath"] = brochurePath;
 }
}

第8步:更新DisplayCategoryPicture.aspx页面

  让我们花几分钟测试我们在上几步创建的插入界面和ItemInserting事件处理器。在浏览器查看UploadInDetailsView.aspx页面 ,尝试添加一个类,忽略picture或指定一个非JPG的图片或非PDF的小册子。以上任何一种情况下,都会显示一个错误信息,并取消插入操作。

/uploads/allimg/c161121/14OI945322960-14O1D.gif
图9:当上传的文件不对时将显示一个警告信息

  确认页面要求上传一张图片,且不接受非PDF或非JPG文件。添加一个包含JPG格式图片的新类别,将Brochure列置空,点击Insert按钮后,页面回传,将为Categories表添加一个新记录,同时上传的图片数据直接存储进数据库。GridView控件更新后,将新添加的类显示出来。但是,就像图10所示的那样,类的图片没有正确的显示出来。

/uploads/allimg/c161121/14OI9453G1P-14W519.gif
图10:新类的图片没有显示出来

  图片没有显示出来的原因是因为用来返回特定类的图片的页面DisplayCategoryPicture.aspx被设置为处理带OLE报头的位图。当Picture列的数据被返回到客户端前已经把那78字节的报头剥离掉。而且上传的JPG文件并没有OLE报头,因此,必需的字节已经从图片的二进制数据移除了。

(编辑:常州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读