Upload Images and Send Them as Email Attachments

原文链接:http://www.cnblogs.com/HenryLiang/archive/2006/06/15/Upload_Image_And_Send_With_Email.html

今天要写一个小web application,允许用户上传最多到3张图片(网页上有3个文件上传框),然后将图片压缩之后添加到email中作为附件,最后用服务器上的smtp server发送email。听起来很简单,但是当中却包含了一个潜在的usability问题。
这个问题发生在,当用户没有使用第一个上传框而是用第二个或是第三个上传框,当我在Iterate httpUploadedFiles时,就会导致第一个上传文件为空。这样当我再将这个空的图片作为附件添加到email的时候,就会导致错误。当然,按照一般的使用规律,用户当然会先选择第一个上传框,然后第二个……但是不能够排除某些时候的特定情况,这也是程序强健性的表现。
这个问题有两种解决方法,
1. 修改页面结构。一开始只显示一个上传框,如果有必要,允许用户“添加下一张图片”,就像hotmail那样。
2. 用程序来检测哪个上传框中真正上传了文件。
我采用了第二个办法,下面是代码:

Upload Images and Send Them as Email AttachmentsDim hpc As HttpFileCollection = HttpContext.Current.Request.Files
Upload Images and Send Them as Email AttachmentsDim ht2 As New Hashtable
Upload Images and Send Them as Email AttachmentsIf Not hpc Is Nothing Then
Upload Images and Send Them as Email Attachments    Dim i As Integer = 0
Upload Images and Send Them as Email Attachments    Dim c As Integer = 0
Upload Images and Send Them as Email Attachments    While c < hpc.Count
Upload Images and Send Them as Email Attachments        Dim file As System.Web.HttpPostedFile = hpc(c)
Upload Images and Send Them as Email Attachments        If (Not file Is Nothing) And (file.ContentLength > 0) Then
Upload Images and Send Them as Email Attachments            Dim str As String = SaveImage(file)
Upload Images and Send Them as Email Attachments            ht2.Add("Image" & i.ToString(), str)
Upload Images and Send Them as Email Attachments            i = i + 1
Upload Images and Send Them as Email Attachments        End If
Upload Images and Send Them as Email Attachments        c = c + 1
Upload Images and Send Them as Email Attachments    End While
Upload Images and Send Them as Email AttachmentsEnd If

转载于:https://www.cnblogs.com/HenryLiang/archive/2006/06/15/Upload_Image_And_Send_With_Email.html

上一篇:git Please move or remove them before you can merge


下一篇:She Left Her Shoes