I do not believe that it is the best solution to process an already uploaded file. Ideally it would just move the file to a folder on your server and have a service that does Processing for you.
This prevents you from locking your application into the ISS and the database by doing a very heavy processing, and takes away that responsibility from your Website or Winforms to do what in fact would not be their responsibility, facilitating future maintenance on a code that is likely to be complex.
There is no way I can suggest an implementation of how to save the File since I don’t know if you use MVC, Web Forms or Win Forms.
But in the case of Windows Service you need to use a File System Watcher object to monitor the receiving of files in the server folder as an example:
public partial class ProcessadorDeArquivo
{
protected FileSystemWatcher _fileSystemWatcher { get; set; }
public ProcessadorDeArquivo()
{
_fileSystemWatcher = new FileSystemWatcher(@"C:\Arquivos"); //Pasta que será utilizada para salva os arquivos.
_fileSystemWatcher.Filter = ".txt" //ExtensãoDoArquivo
_fileSystemWatcher.Created += FileSystemWatcherCreated;
_fileSystemWatcher.EnableRaisingEvents = true;
}
/// <summary>
/// Quando um arquivo é criado na pasta assistida esse evento é disparado
/// </summary>
protected void FileSystemWatcherCreated(object sender, FileSystemEventArgs e)
{
ProcessarArquivos(e.FullPath); //Método que teria toda a regra de processar.
}
}
EDIT (ASP.NET MVC)
//VIEW
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype ="multipart/form- data" }))
{
<input type="arquivo" name="arquivo" />
<input type="submit" value="salvar" />
}
//CONTROLLER
public ActionResult Index(HttpPostedFileBase arquivo)
{
// Verify that the user selected a file
if (arquivo != null)
{
var nome = Path.GetFileName(arquivo.FileName);
arquivo.SaveAs(@"C:\Arquivos\" + nome);
}
return View();
}
Are you looking to process the file in C# while uploading? That is, before the upload is finished, do you already want C# to be processing the file? That’s it?
– Miguel Angelo
Don’t do that. There are so many things that can go wrong. You can control the timeout of IIS and ASP.NET but you cannot control the browser timeout. ASP.ET was not made for heavy processing. You should immediately put the file in a BD or disk and have an external service to process it. To give information to the user you could use Polling or use Signalr to notify.
– Paulo Morgado
Google search suggestion "Async file upload Asp.net mvc". Which will lead you to the use of asyncronos methods. One of the results I found in English was https://damienbod.wordpress.com/2013/09/03/mvc-async-file-upload/
– Guilherme de Jesus Santos