1 / 17

第13章 FTP 檔案上傳、 Email 自動傳送

第13章 FTP 檔案上傳、 Email 自動傳送. 報告人 M9153314 李育旻. FTP 檔案上傳. 檔案上傳的發動在瀏覽器這一端,在設計程式時,要注意 表單 Enctype (編碼型態)屬性須設定成” multipart/form-data” 佈置 File 型態的 HTML 輸入欄位,供使用者選取所要上傳的檔案. <Html> <Body BgColor=White> <H3> 檔案上傳 -- 上傳一個檔案 <Hr></H3> <Form Enctype="multipart/form-data" runat="server">

marla
Download Presentation

第13章 FTP 檔案上傳、 Email 自動傳送

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第13章FTP檔案上傳、Email自動傳送 報告人 M9153314 李育旻

  2. FTP檔案上傳 • 檔案上傳的發動在瀏覽器這一端,在設計程式時,要注意 • 表單Enctype(編碼型態)屬性須設定成”multipart/form-data” • 佈置File型態的HTML輸入欄位,供使用者選取所要上傳的檔案

  3. <Html> <Body BgColor=White> <H3>檔案上傳 -- 上傳一個檔案<Hr></H3> <Form Enctype="multipart/form-data" runat="server"> 請輸入完整檔案路徑: <Input Type="File"id="FileUp" runat="server"><P> <asp:Button runat="server" Text="Upload" OnClick="UploadFile" /> <Hr> <asp:Label runat="server" id="Msg" /> </Form> </Body> </Html> FileUp Msg

  4. <script language="VB" runat="server"> Sub UploadFile(sender As Object, e As EventArgs) Dim file As HttpPostedFile = FileUp.PostedFile If file.ContentLength <> 0 Then Msg.Text = "大小: " & file.ContentLength Msg.Text &= "<br>類型: " & file.ContentType Msg.Text &= "<br>名稱: " & file.FileName Dim FileSplit() As String = Split( file.FileName, "\" ) Dim FileName As String = FileSplit(FileSplit.Length-1) file.SaveAs( Server.MapPath(FileName) ) Else Msg.Text = "" End If End Sub </script> FileUp Msg

  5. FTP上傳多個檔案 <Html> <Body BgColor=White> <H3>檔案上傳 -- 上傳多個檔案<Hr></H3> <Form Enctype="multipart/form-data" runat="server"> 請輸入完整路徑檔案名稱:<br> <Input Type="File" id="FileUp1" runat="server"><br> <Input Type="File" id="FileUp2" runat="server"><br> <Input Type="File" id="FileUp3" runat="server"><br> <asp:button runat="server" Text="Upload" OnClick="UploadFile" /> <Hr> <asp:Label runat="server" id="Msg" /> </Form></Body></Html> • 若想要同時上傳多個檔案,要在表單中佈置多個<Input type=“FILE”>欄位

  6. <script language="VB" runat="server"> Sub UploadFile(sender As Object, e As EventArgs) Dim I As Integer Msg.Text = "" For I = 0 To Request.Files.Count - 1 Dim File As HttpPostedFile = Request.Files(I) If File.ContentLength > 0 Then ' 顯示上傳檔案的資訊 Msg.Text &= "檔案: " & File.FileName Msg.Text &= ", " & File.ContentLength Msg.Text &= ", " & File.ContentType & "<br>" ' 儲存檔案 Dim FileSplit() As String = Split( File.FileName, "\" ) Dim FileName As String = FileSplit(FileSplit.Length-1) File.SaveAs( Server.MapPath(FileName) ) End If Next End Sub </script>

  7. Email自動傳送 • 網頁製做中,需要自動傳送Email給上網者的情況很多,例如: • 電子購物網頁:上網者購買物品後,傳送物品清單供上網者備查 • 密碼網頁:在需要登入帳號及密碼的網頁中,上網者如果忘了密碼,我們可提供一個網頁讓上網者輸入原先註冊之Email,然後系統自動將密碼Email給此一郵件位址,上網者再去收信,便可知道密碼為何 • 電子賀卡:上網者可選擇卡片寄給某人,我們要實做寄信給受卡者的動作

  8. MailMessages及SmtpMail物件 <script Language="VB" runat="server"> Sub Button_Click(sender As Object, e As EventArgs) Dim mail As MailMessage = New MailMessage mail.To = 收件人e-mail mail.From = 送件人e-mail mail.Subject = 主旨 mail.BodyFormat = 郵件格式(純文字或HTML) mail.Body = 郵件內容 On Error Resume Next SmtpMail.SmtpServer = “smtp伺服器的位址" SmtpMail.Send(mail) If Err.Number <> 0 Then Msg.Text = Err.Description Else Msg.Text = "郵件已經送出!" End If End Sub </script> • MailMessages用來設定Email的內容 • SmtpMail負責將Email傳送出去

  9. mailTo mailFrom mailSubject mailBody

  10. <%@ Import Namespace="System.Web.Mail" %> <Html> <Body BgColor="White"> <H2>ASP.NET Email 發送程式!<Hr></H2> <Form runat="server"> <Table Border=1> <Tr><Td>收件者:</Td> <Td><asp:TextBox id="mailTo" Size=40 runat="server"/></Td></Tr> <Tr><Td>寄件者:</Td> <Td><asp:TextBox id="mailFrom" Size=40 runat="server"/></Td></Tr> <Tr><Td>主旨:</Td> <Td><asp:TextBox id="mailSubject" Size=40 runat="server"/></Td></Tr> <Tr><Td>內文:</Td> <Td><asp:TextBox runat="server" id="mailBody" TextMode="MultiLine" Rows=8 Cols=60 /> </Td></Tr> </Table> <asp:Button runat="server" Text="送 出" OnClick="Button_Click" /> </Form> <Hr><asp:Label id="Msg" runat="server" ForeColor="Red" /><p> <Font Size=-1 Color=Blue>使用本範例之前,請先參閱書本「SMTP Server 與郵件的傳送」段落中的說明,設定好 SmtpMail.SmtpServer 屬性。</Font> </Body> </Html>

  11. <script Language="VB" runat="server"> Sub Button_Click(sender As Object, e As EventArgs) Dim mail As MailMessage = New MailMessage mail.To = mailTo.Text mail.From = mailFrom.Text mail.Subject = mailSubject.Text mail.BodyFormat = MailFormat.Text mail.Body = mailBody.Text On Error Resume Next SmtpMail.SmtpServer = "msa.hinet.net" SmtpMail.Send(mail) If Err.Number <> 0 Then Msg.Text = Err.Description Else Msg.Text = "郵件已經送出!" End If End Sub </script>

  12. <script Language="VB" runat="server"> Sub Button_Click(sender As Object, e As EventArgs) Dim mail As MailMessage = New MailMessage mail.To = mailTo.Text mail.From = mailFrom.Text mail.Subject = mailSubject.Text mail.BodyFormat = MailFormat.Html mail.Body = mailBody.Text On Error Resume Next SmtpMail.SmtpServer = "msa.hinet.net" SmtpMail.Send(mail) If Err.Number <> 0 Then Msg.Text = Err.Description Else Msg.Text = "郵件已經送出!" End If End Sub

  13. MailMessage的細部設定 • 原始收件人格式mail.To=“kjwang@gcn.net.tw” • 設定收件人姓名 • mail.To = “王國榮<kjwang@gcn.net.tw>” • mail.From = “阿毛<amou@kjwang.com>” • 多位收件人 • mail.To = “王國榮<kjwang@gcn.net.tw>, 阿毛<amou@kjwang.com>” • 副本收件人 • mail.Cc = “阿毛<amou@kjwang.com>” • 密件副本收件人 • mail.Bcc = “阿毛<amou@kjwang.com>”

  14. Email增加附件 • 若要將檔案附加到Email中一起傳送,所需撰寫的程式如下: • Dim attach as New MailAttachment(完整路徑檔案名稱) • mail.Attachments.Add(attach) • ……. • SmtpMail.Send(mail) • 程式碼解說 • 利用New MailAttachment()建立一個附件,建立時須傳入完整路徑檔案名稱。例如目前目錄有F8315.gif 檔案要設定成附件,則敘述如下 • Dim attach As New MailAttachment(Server.MapPath(“F8315.gif”)) • 2. 建立附件後,將其加入Email中須呼叫Attachments.Add方法 • mail.Attachments.Add(attach)

  15. 範例 • 新增<Input type=“File” , id=“FileUp” run=“server”> • 表單屬性增加Enctype設定,如下 • <Form enctype=“multipart/form-data” run=“server”>

  16. 程式碼解說 Sub Button_Click(sender As Object, e As EventArgs) Dim mail As MailMessage = New MailMessage mail.To = mailTo.Text mail.From = mailFrom.Text mail.Subject = mailSubject.Text mail.Body = mailBody.Text If Format.SelectedItem.Text = "純文字格式" Then mail.BodyFormat = MailFormat.Text Else mail.BodyFormat = MailFormat.Html End If Dim file As HttpPostedFile = FileUp.PostedFile If file.ContentLength <> 0 Then Dim FileSplit() As String = Split( File.FileName, "\" ) Dim FileName As String = FileSplit(FileSplit.Length-1) File.SaveAs( Server.MapPath(FileName) ) Dim attach As New MailAttachment(Server.MapPath(FileName)) mail.Attachments.Add( attach ) End If On Error Resume Next SmtpMail.SmtpServer = "msa.hinet.net" SmtpMail.Send(mail)

  17. 結束

More Related