文件上载在Safari浏览器中无法正常工作 [英] File Upload is not working properly in Safari Browser

查看:185
本文介绍了文件上载在Safari浏览器中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp.net页面用于发送文件作为附件。

我正在使用文件上传控件选择多个文件并将其保存在一个文件夹中并从该文件夹中将该文件作为附件发送给邮件。

这样工作正常在谷歌浏览器和IE浏览器上,但Safari浏览器文件正在以0字节大小复制。如果我们选择1个单个文件作为附件,那么它在所有浏览器中都能正常工作。



提前致谢。



发送代码按钮



 使用(MailMessage) mailMessage =  new  MailMessage())
{
mailMessage.From = new MailAddress ( myname@domain.com);
mailMessage.Subject = txtSubject.Text.Trim();
mailMessage.Body = txtMessage.Text.Trim();
mailMessage.IsBodyHtml = true ;
mailMessage.To.Add( new MailAddress(txtTo.Text.Trim()));


// 将所有选择的文件复制到EmailFiles文件夹以便发送为邮件的附件。

if (FileUpload1.HasFile || FileUpload1.FileName!=
{
foreach (HttpPostedFile uploadedFile in FileUpload1.PostedFiles)
{
uploadedFile.SaveAs(System.IO.Path.Combine(Server.MapPath( 〜/ EmailFiles /),uploadedFile.FileName));
}
}
// 加密文件

string encryptFilePath = Request.PhysicalApplicationPath + EmailFiles\\;
string [] S2 = Directory.GetFiles(encryptFilePath);
foreach 字符串 fileName in S2)
{
encryptFiles(fileName);
}

// 循环遍历所有文件以将文件作为附件发送。
string UplodedfilesForEmail = Request.PhysicalApplicationPath + EmailFiles\\;
string [] S1 = Directory.GetFiles(UplodedfilesForEmail);
foreach 字符串 fileName in S1)
{
mailMessage.Attachments.Add( new Attachment(fileName));
}

SmtpClient smtp = new SmtpClient();
smtp.Host = mail.domain.com;
smtp.EnableSsl = true ;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential( emailID < password>);
NetworkCred.UserName = mailMessage.From.Address;
NetworkCred.Password = < password>;
smtp.UseDefaultCredentials = true ;
smtp.Credentials = NetworkCred;
ServicePointManager.ServerCertificateValidationCallback = delegate object s,X509Certificate certificate,X509Chain chain,SslPolicyErrors sslPolicyErrors)
{返回 true ; };
smtp.Port = 25 ;
smtp.Send(mailMessage);
}

解决方案

在附加到电子邮件内容之前,不要将文件复制到服务器上的物理位置,而是执行此操作直接通过内存流。



查看codeproject文章,将上传的文件从内存流附加到电子邮件附件。



创建内存邮件附件 [ ^ ]


< blockquote>读一读这个



http://stackoverflow.com/questions/15766644/any-workarounds-for-the-safari-html5-multiple-file-upload-bug [ ^ ]


它好像是一个safari浏览器StackOverflow提到的问题

http://stackoverflow.com/questions/7231054/file-input-size-issue-in-safari-for-multiple-file-selection [ ^ ]



检查所有浏览器上的fiddler链接并查看大小属性

http: //jsfiddle.net/rHd26/6/ [ ^ ]

I have one asp.net page for sending files as an attachment.
I am selecting multiple files using file upload control and saving it in one folder and from that folder i m sending that files as an attachment with a mail.
This is working fine on google chrome and IE but on "Safari" browser files are getting copied with "0" byte size. If we are selected 1 single file as an attachment then its working fine in all the browsers.

Thanks in advance.

Code of Send Button

using (MailMessage mailMessage = new MailMessage())
                {
                    mailMessage.From = new MailAddress("myname@domain.com");
                    mailMessage.Subject = txtSubject.Text.Trim();
                    mailMessage.Body = txtMessage.Text.Trim();
                    mailMessage.IsBodyHtml = true;
                    mailMessage.To.Add(new MailAddress(txtTo.Text.Trim()));


                    // Copy all seleted files to "EmailFiles" folder for sending as an attachment to mail.

                    if (FileUpload1.HasFile || FileUpload1.FileName !="")
                    {
                        foreach (HttpPostedFile uploadedFile in FileUpload1.PostedFiles)
                        {
                            uploadedFile.SaveAs(System.IO.Path.Combine(Server.MapPath("~/EmailFiles/"), uploadedFile.FileName));                        
                        }
                    }
                    // Encrypt files

                    string encryptFilePath = Request.PhysicalApplicationPath + "EmailFiles\\";
                    string[] S2 = Directory.GetFiles(encryptFilePath);
                    foreach (string fileName in S2)
                    {
                        encryptFiles(fileName);
                    }
                    
                    // looping through all the files to send files as an attachment.
                    string UplodedfilesForEmail = Request.PhysicalApplicationPath + "EmailFiles\\";
                    string[] S1 = Directory.GetFiles(UplodedfilesForEmail);
                    foreach (string fileName in S1)
                    {
                        mailMessage.Attachments.Add(new Attachment(fileName));
                    }
                                        
                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = "mail.domain.com";
                    smtp.EnableSsl = true;
                    System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential("emailID", "<password>");
                    NetworkCred.UserName = mailMessage.From.Address;
                    NetworkCred.Password = "<password>";
                    smtp.UseDefaultCredentials = true;
                    smtp.Credentials = NetworkCred;
                    ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
                    { return true; };
                    smtp.Port = 25;
                    smtp.Send(mailMessage);
                }

解决方案

Instead of copying the files to a physical location on your server before attaching to the email content, do it directly through memory stream.

Check the codeproject article to attach an uploaded file from memory stream to email attachment.

Creating In-Memory Mail Attachments[^]


Have a read of this

http://stackoverflow.com/questions/15766644/any-workarounds-for-the-safari-html5-multiple-file-upload-bug[^]


It seems to be like a safari browser issue mentioned at StackOverflow
http://stackoverflow.com/questions/7231054/file-input-size-issue-in-safari-for-multiple-file-selection[^]

Check the fiddler link on all browsers and see the "Size" attribute
http://jsfiddle.net/rHd26/6/[^]


这篇关于文件上载在Safari浏览器中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆