0

I would like to pass a file into a [Webmethod] and once its inside, send it as an email attachment.

How would i go about doing this? I'm unsure where to start?

FileUpload1.PostedFile.FileName and FileUpload1.FileContent are the 2 things i want to pass into my [webmethod].

My Problem is the FileUpload1.FileContent, because i can already send my FileUpload1.PostedFile.FileName as a String

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'

Please could someone show me how i can send FileUpload1.FileContent into a [Webmethod]

Thanks in advance.

Pomster
  • 14,567
  • 55
  • 128
  • 204
  • To see what i'm trying to do, check out my question :http://stackoverflow.com/q/10431928/1356321 This will let you see what code i have thus far. – Pomster May 04 '12 at 06:01
  • 1
    IMO, the "correct" way to serialize and deserialize an `UploadFile` is... to not do that. If you need to pass a filename and data, then by all means do that - but that doesn't need `UploadFile` – Marc Gravell May 04 '12 at 06:09
  • Ok so how do i pass the FileUpload1.FileContent to the Webmethod???, I need to attach a file to an email so i need uploadfile – Pomster May 04 '12 at 06:16
  • no, you need to send data; that does not need to be done via an `UploadFile`; a base-64 string (or `byte[]` if it works) would be much simpler – Marc Gravell May 04 '12 at 06:31
  • Ok how do i send data? i am reading up on base-64 and Byte[] how do i contvert a file into that? and is the conversion not called serializeing the data? – Pomster May 04 '12 at 06:32
  • You have been no help at all, basically answered my question with [no] and have not redirected me in any helpful way? You neged my question with -1 point suggesting i have done no research effort, but i have 3 question asking for help of this same topic, and there is a link there to redirect you, its a very detailed question to something i do believe will be a easy answer its just something i don't understand. so thanks for no help the -1 will chase people who might help away! – Pomster May 04 '12 at 06:48
  • 1
    which "you" are you referring to? The -1 is not by me, not that it matters. Do you expect me to apologise for stepping away from my PC for a moment? Sending data: the same as how you normally send data on http; http body, http form, query-string or header. Pick any. More generally: the question is "how do I [some already chosen, perhaps misadvised, implementation]?" - I'm saying you need to take a step back: "how do I [the thing I want to *achieve*]?". Comparison: "what is the best hammer for driving in screws?" – Marc Gravell May 04 '12 at 06:51
  • Sorry, I'm just frustrated because i cant find any help for these questions. I cant back track i have spent to much time on this project already, and have finished it, now i need to put the functionality into a [webmethod] if you follow the link you can see what i'm trying to Achieve :stackoverflow.com/q/10431928/1356321 – Pomster May 04 '12 at 07:04
  • I'm not really setup to repro that etc, but I would try it as a `byte[]` instead of a `Stream`. Are you familiar with `Stream` processing to fill the `byte[]` at the caller? Note also this will inevitably involve base-64, so will have a little overhead. To get back to a `Stream` at the receiver: `stream = new MemoryStream(theByteArray)` – Marc Gravell May 04 '12 at 07:07
  • ok so how do i Convert my stream into byte[]? and no i am unfamiliar to stream processing. so something like byte[] file = (FileUpload1.FileContent); and then pass it though? – Pomster May 04 '12 at 07:14

1 Answers1

2

My advice is: forget FileUpload - just pass a byte[] and string (for the name, or whatever else you need). This should get handled as base-64 by the system.

From a FileUpload you should have access to the .FileBytes and .FileName, avoiding the need to mess with Stream etc.

At the receiving end (with a byte[] parameter), simply:

using(var stream = new MemoryStream(theByteArray)) {...}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900