Realizando Upload e download de arquivos
Olá bom dia a todos. Eu realizei o tutorial da Devmedia "ASP.NET MVC 4: Realizando Upload e download de arquivos" porém gostaria de acrescentar a implementação para deletar os arquivos que realizei o Upload e também ordenar de forma decrescente. Se possível gostaria que alguém pudesse me ajudar pois sou iniciante em aspnet. Desde já agradeço
Grasielly Silva
Curtidas 0
Respostas
Stella Oliveira
25/08/2019
Olá bom dia a todos. Eu realizei o tutorial da Devmedia "ASP.NET MVC 4: Realizando Upload e download de arquivos" porém gostaria de acrescentar a implementação para deletar os arquivos que realizei o Upload e também ordenar de forma decrescente. Se possível gostaria que alguém pudesse me ajudar pois sou iniciante em aspnet. Desde já agradeço
Para ordenar o arquivo eu faria da seguinte forma:
try
{
var provider = new MultipartMemoryStreamProvider();
await Request.Content.ReadAsMultipartAsync(provider);
foreach (var file in provider.Contents)
{
var buffer = await file.ReadAsStreamAsync();
List<string[]> arquivoGlobal = new List<string[]>();
using (var reader = new StreamReader(buffer, Encoding.Default, true))
{
string conteudo;
while ((conteudo = reader.ReadLine()) != null)
{
string[] linhas = conteudo.Split('';'');
arquivoGlobal.Add(linhas);
}
}
var qtdColunas = arquivoGlobal.First().Count();
arquivoGlobal.RemoveAt(0);
arquivoGlobal = arquivoGlobal.OrderBy(x => x[0]).ThenBy(x => x[2]).ToList();
}
}catch(Exception ex) {
string erro = ex.Message;
}
E para deletar:
// Specify the directories you want to manipulate.
string path = @"c:\\\\MyDir";
string target = @"c:\\\\TestDir";
try
{
// Determine whether the directory exists.
if (!Directory.Exists(path))
{
// Create the directory it does not exist.
Directory.CreateDirectory(path);
}
if (Directory.Exists(target))
{
// Delete the target to ensure it is not there.
Directory.Delete(target, true);
}
// Move the directory.
Directory.Move(path, target);
// Create a file in the directory.
File.CreateText(target + @"\\\\myfile.txt");
// Count the files in the target directory.
Console.WriteLine("The number of files in is ",
target, Directory.GetFiles(target).Length);
}
catch (Exception e)
{
Console.WriteLine("The process failed: ", e.ToString());
}
GOSTEI 0