Click here to Skip to main content
15,919,613 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I am trying to migrate some Winforms code to ASP.NET MVC, and this line:

C#
File.WriteAllBytes(pdfFile, bytes);


...won't compile, telling me, "'System.Web.Mvc.Controller.File(byte[], string)' is a 'method', which is not valid in the given context"

Is it possible to accomplish the same thing (write a file to disk) in ASP.NET MVC as can be done with a Winforms app? I'm assuming it's a security issue that disallows this, but it is possible to download files to the client, so I assume something similar can be done by writing the bytes to a stream...but how, exactly?

If this is not feasible/possible, I reckon I will store the bytes in a database table and then make the file available from there via a link.

What I have tried:

I scratched my head, but nothing fell out, except the idea about saving the data to a table.
Posted
Updated 3-May-16 12:44pm
Comments
F-ES Sitecore 4-May-16 6:08am    
Use "System.IO.File.WriteAllBytes" as there is a namespace conflict. This really is basic .net 101, and "converting" a winforms app to a web app is a massively complex thing to do and if you don't understand the basics like namespaces etc you're really going to struggle.

As already mentioned, you can't write files to the client like that if that is what you are intending, you can only write them to the server.

1 solution

You cannot save anything on the user's (client's) computer, but the user can save everything.

You got the answer; and not think for a second: in an alternative Universe, where Web application would be able to save anything the developer wants on clients' computers, how many would like to use such kind of Internet? :-)

Now, what is unclear in the error message you get? Look at System.Web.Mvc.Controller.File:
Controller.File Method (System.Web.Mvc)[^].

This is a method. The trouble starts when you add .* to the method's name. I don't think it needs any explanation.

Apparently, you messed it up with System.IO.File:
File Class (System.IO)[^].

And of course its method WriteAllBytes could save anything only on the server side, as well as any other methods.

[EDIT]

But it does not mean you cannot deliver any file to the client site. Of course you can; and this is an elementary thing. Actually, the whole purpose of all server-side technology is to do it — to generate some HTTP response on the fly and transmit any data.

But it can be done only on some HTTP request, which you can actually generate of some page, by obvious means. For simplicity, let's assume you create some anchor, so the user clicks on it to receive the file. A trivial way is to create a file on a server side and provide a link on it in that anchor. But, apparently, this is not what you want. The link could be an ASP.NET code which generates HTTP response on the fly. You generate content based on some data, create appropriate header, such as "application/zip" or "application/json" (Media Types[^]), write content in the network stream of the HTTP response:
HttpResponse Class (System.Web)[^],
HttpResponse.ContentType Property (System.Web)[^],
HttpResponse.ContentEncoding Property (System.Web)[^],
HttpResponse.OutputStream Property (System.Web)[^].

[END EDIT]

What can I say? Sorry, but with this level of understanding such thins as syntax, error message, namespaces and other very basic and simple things, and also without the basic idea of what Web does, it's hard to imagine any productive ASP.NET MVC development. Listen to a good friendly advice: learn he basics of language, .NET and general programming first; for this purpose, programming of simplest console-only applications would be the best.

I really cannot understand it. 16 articles with "Legend" status, many of them are on ASP.NET… This post is hard to explain by a little random mistake; the error message is recognized in a second. Are you trying to mystify us?

—SA
 
Share this answer
 
v8

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900