正确的方法来传递文件上传内容转换成[的WebMethod] [英] The Correct way to pass FileUpload content into [WebMethod]?

查看:266
本文介绍了正确的方法来传递文件上传内容转换成[的WebMethod]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个[的WebMethod] Sendemail这工作得很好,但现在我该怎么升级它来发送附件。我使用的是文件上传。这是我的方法调用。

  lblEmailSent.Text = Send.Sendemail(txtTo.Text,txtSubject.Text,txtbody.Text, FileUpload1.PostedFile.FileName,FileUpload1.FileContent); 



我的CALL语句中带下划线的蓝色和两个错误给出如下:




*的 1)的*为'WebTestServiceApp.localhost.Service1.Sendemail(字符串,字符串,
的最佳重载的方法匹配字符串,字符串WebTestServiceApp.localhost.Stream)'有一些无效的
参数



*的 2)的*参数5:不能转换从的System.IO.Stream'到'WebTestServiceApp.localhost.Stream




FileUpload1.PostedFile.FileName作为字符串传递FileUpload1 .FileContent作为流传递



这是我的[的WebMethod],现在ü所有能看到的每一件事,我可以看到,我看不出什么毛病,但我我不确定如果FileUpload1.FileContent应作为流进行传递。

  [的WebMethod] 
公共字符串Sendemail(字符串inValueTo ,字符串inValueSub,字符串inValueBody,字符串inValueAttachmentPostedfile,流inValueAttachemtnFileContent)//字符串inValueAttachmentPostedfile,流inValueAttachemtnFileContent
{

{
字符串valueTo = inValueTo;
字符串valueSub = inValueSub;
字符串valueBody = inValueBody;
字符串valueAttachmentPostedfile = inValueAttachmentPostedfile; //FileUpload1.PostedFile.FileName
流valueAttachmentFileContent = inValueAttachemtnFileContent; //FileUpload1.FileContent.fileName

System.Net.Mail.MailMessage消息=新System.Net.Mail.MailMessage(); //创建新的消息。

message.To.Add(valueTo);
message.Subject = valueSub;
message.From =新System.Net.Mail.MailAddress(shaunmossop@mweb.co.za);
message.Body = valueBody;
message.IsBodyHtml = TRUE;

字符串文件名= Path.GetFileName(valueAttachmentPostedfile); //获取附件文件
扣押myAttachment =
新的附件(valueAttachmentFileContent,文件名);
如果(文件名=!)
{
message.Attachments.Add(myAttachment); //发送附件
}

System.Net.Mail.SmtpClient SMTP =新System.Net.Mail.SmtpClient(smtp.gmail.com); //Properties.Settings.Default.MailSMTPServer

smtp.Port = 587;
smtp.EnableSsl = TRUE;
smtp.UseDefaultCredentials = FALSE;

的NetworkCredential NETC =新的NetworkCredential(Properties.Settings.Default.username,Properties.Settings.Default.password); //期运用项目defult设置。
smtp.Credentials = NETC;
smtp.Send(消息);

返回消息已发送
}
赶上(例外)
{
返回消息faild派;

}
}


解决方案

它给你击中了要害的第二个错误,是路过的youre串串串字符串,但传递的youre错误类型的流为它进行交互的,它希望这种WebTestServiceApp.localhost.Stream
您选择给它这种
的System.IO.Stream



在使用流尽量明确地宣布它为WebTestServiceApp.localhost.Stream当你第一次使它,这样的youre然后传递正确类型的流,你的问题应该停止!



那里有两种类型,你看到流,用于Web应用程序之一,用于桌面设备应用。


I have a [WebMethod] Sendemail This works fine but now i what to upgrade it to send attachments. I am using a fileupload. This is my method Call.

lblEmailSent.Text = Send.Sendemail(txtTo.Text, txtSubject.Text, txtbody.Text, FileUpload1.PostedFile.FileName, FileUpload1.FileContent);

My Call Statement is underlined in blue and the two error given look like:

*1)*The best overloaded method match for 'WebTestServiceApp.localhost.Service1.Sendemail(string, string, string, string, WebTestServiceApp.localhost.Stream)' has some invalid arguments

*2)*Argument 5: cannot convert from 'System.IO.Stream' to 'WebTestServiceApp.localhost.Stream'

FileUpload1.PostedFile.FileName is passed as a String FileUpload1.FileContent is passed as a stream

This is my [WebMethod], Now u all can see every thing i can see, i can't spot anything wrong, But i am unsure if FileUpload1.FileContent should be passed as a Stream.

[WebMethod]
public string Sendemail(String inValueTo, String inValueSub, String inValueBody, String inValueAttachmentPostedfile, Stream inValueAttachemtnFileContent) //, String inValueAttachmentPostedfile, Stream inValueAttachemtnFileContent
{
    try
    {
        String valueTo = inValueTo;
        String valueSub = inValueSub;
        String valueBody = inValueBody;
        String valueAttachmentPostedfile = inValueAttachmentPostedfile; //FileUpload1.PostedFile.FileName
        Stream valueAttachmentFileContent = inValueAttachemtnFileContent;  //FileUpload1.FileContent.fileName

        System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); // Creating new message.

        message.To.Add(valueTo);
        message.Subject = valueSub;
        message.From = new System.Net.Mail.MailAddress("shaunmossop@mweb.co.za");
        message.Body = valueBody;
        message.IsBodyHtml = true;

          string fileName = Path.GetFileName(valueAttachmentPostedfile); // Get attachment file
         Attachment myAttachment =
                         new Attachment(valueAttachmentFileContent, fileName);
          if (fileName != "")
          {
              message.Attachments.Add(myAttachment); // Send attachment
          }

        System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com"); //Properties.Settings.Default.MailSMTPServer

        smtp.Port = 587;
        smtp.EnableSsl = true;
        smtp.UseDefaultCredentials = false;

        NetworkCredential netC = new NetworkCredential(Properties.Settings.Default.username, Properties.Settings.Default.password); // Useing Projects defult settings.
        smtp.Credentials = netC;
        smtp.Send(message);

        return "Message has been sent";
    }
    catch (Exception)
    {
        return "Message faild to send" ;

    }
}

解决方案

the second error it gives you hits the nail on the head, yes youre passing string string string string but youre passing the wrong type of stream for it to interact with, it wants this kind WebTestServiceApp.localhost.Stream youre giving it this kind System.IO.Stream

when you use the stream try explicitly declaring it as a WebTestServiceApp.localhost.Stream when you first make it, that way youre then passing the right types of stream and your problem should stop!

theres two types of stream you see, one used for web apps and one used for desktop apps.

这篇关于正确的方法来传递文件上传内容转换成[的WebMethod]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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