Why can't I use System.IO.File methods in an MVC controller?
You should prefix it with a namespace:
if (System.IO.File.Exists(picPath))
{
//Other code
}
The reason for that is because you are writing this code inside a controller action which already defines a File
method on the Controller class.
How to access the static System.IO.File class from an MVC Controller?
In place of File.Delete(path)
which, within the context of a controller will use the controllers version of the method; you need to use System.IO.File.Delete()
exactly like that to access the system input/output version (which is the one you need). If you don't specify System.IO.File.
before delete
it will default to the controllers version even if you have a using statement for System.IO
at the top of your file.
How Can I Use File class in an MVC Controller in ASP.Net?
You can use the full namespace to explicitly point at the correct File
class:
using (var fileStream = System.IO.File.Create(path))
File is a Method which is not valid in the given context
You probably have this method inside MVC controller in which File method exists. Add in your code System.IO.File
instead of File
Unable to use File.WriteAllBytes() in asp.net MVC, cannot choose method from method group did you intend to invoke the method?
It sounds like you have a method that's visible to you (in the same class, say) called File
. This will be chosen in preference to System.IO.File
. So you need to fully qualify the name:
System.IO.File.WriteAllBytes(tempfilenameandlocation, thePDFLetter);
(In fact, enough clues were in the question. It's Controller.File
that's in scope)
C# to ASP.NET MVC FileStream Crossover
You'll have to add the fully-qualified name if you want to use the File
class in System.IO
(http://msdn.microsoft.com/en-us/library/system.io.file.aspx). So something like this should work:
using (FileStream stream = System.IO.File.OpenRead("database.dat")){
database = (List)formatter.Deserialize(stream);
}
ControllerBase.File(byte[], string)' is a method, which is not valid in the given context (CS0119) - in method
1) It is ok to put uploading into separate method, it could also be put into a separate class for handling blob operations
2) File
is the name of one of the controllers methods, if you want to reference the File
class from System.IO namespace, you need to fully qualify the name
FileStream uploadFileStream = System.IO.File.OpenRead(localFilePath);
To the other compile error, you need to return something from the UploadToBlob
method, now it does not return anything from the try
block
3) File type validation can be put into the controller action method
4) it depends on what you plan to do with the text and how are you going to use it. Would it be a new action of the controller (a new API endpoint)?
5) you could create a new API endpoint for downloading files
UPDATE:
For word replacement you could use a similar method:
private Stream FindMostFrequentWordAndReplaceIt(Stream inputStream)
{
using (var sr = new StreamReader(inputStream, Encoding.UTF8)) // what is the encoding of the text?
{
var allText = sr.ReadToEnd(); // read all text into memory
// TODO: Find most frequent word in allText
// replace the word allText.Replace(oldValue, newValue, stringComparison)
var resultText = allText.Replace(...);
var result = new MemoryStream();
using (var sw = new StreamWriter(result))
{
sw.Write(resultText);
}
result.Position = 0;
return result;
}
}
it would be used in your Post method this way:
using (var stream = formFile.OpenReadStream())
{
var streamWithReplacement = FindMostFrequentWordAndReplaceIt(stream);
// Upload the replaced text:
(uploadSuccess, uploadedUri) = await UploadToBlob(formFile.FileName, null, streamWithReplacement);
}
Related Topics
Wpf Binding a Listbox to an Enum, Displaying the Description Attribute
C# Base64 String to Jpeg Image
Getting/Setting File Owner in C#
C#: Difference Between "System.Object" and "Object"
How Can a Windows Service Start a Process When a Timer Event Is Raised
What Are the Benefits of Using C# VS F# or F# VS C#
How to Convert List<String> to List<Int>
Is There Anything Like Asynchronous Blockingcollection<T>
How to Get and Set the Window Position of Another Application in C#
How Much Memory Does a C#/.Net Object Use
How to Invoke a UI Method from Another Thread
Configure JSON.Net to Ignore Datacontract/Datamember Attributes
Using Layers and Bitmask with Raycast in Unity
Throw Httpresponseexception or Return Request.Createerrorresponse
Why Does the Contains() Operator Degrade Entity Framework's Performance So Dramatically