0
Good people, I have the following problem. The controller receives an Httppostedfilebase and sends it to Azure and for that I need to write to a temporary folder.
I would like to know a more elegant solution where you do not need to write on the Server disk. In short, send Httppostedfilebase directly to Azure with Cloudfile. Because this solution seems to me to be more performative, in case any ideas please share.
Follows code below:
CloudStorageAccount _azureFile;
public UploadArquivo()
{
_azureFile = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("azureFile"));
}
public void uploadArquivoAzure(HttpPostedFileBase arquivo, string caminhoArquivo)
{
//Escreve arquivo no caminho temporario
arquivo.SaveAs(caminhoArquivo);
// Criando o cliente do azure para acessar o storage
CloudFileClient clienteArquivos = _azureFile.CreateCloudFileClient();
// acessando o serviço de arquivos
CloudFileShare compartilhamentoArquivos = clienteArquivos.GetShareReference("servicoArquivo");
if (compartilhamentoArquivos.Exists())
{
// cria o diretorio raiz
CloudFileDirectory dirRaiz = compartilhamentoArquivos.GetRootDirectoryReference();
// criando o diretorio especifico
CloudFileDirectory diretorio = dirRaiz.GetDirectoryReference("compartilhamentoArquivos");
if (diretorio.Exists())
{
//Setando o arquivo e caminho do mesmo caminho do arquivo
CloudFile arquivoEnviado = diretorio.GetFileReference(arquivo.FileName);
arquivoEnviado.UploadFromFile(arquivo.);
//arquivoEnviado.DownloadToFile(caminhoArquivo, FileMode.Create);
//arquivoEnviado.UploadFromFile(caminhoArquivo);
}
}
}
Any hint would help a lot.
First of all, thank you very much for your reply. In your example in the method "Gravarnamemoria" in tests the same is not returning content as stream, I see that you put a vector of bytes and do not use it, in analyzing the stream I see that you have a buffer, however I could not recover the same, could help in this case. As for using blob I haven’t quite figured out the big differences between blob and file, I’m studying to improve understanding and its placement already help a lot. Inclusive I’ve found File. Thank you.
– Napoleão Menezes
@Napoleon Menezes, that’s correct. I changed the answer code with an excerpt of my old code. Some things were really missing.
– Thiago Lunardi
@Napoleon Menezes, as for the difference between using Blob and File. File vc uses to synchronize a file on disk with the cloud, as if it were going to make a remote drive. In Blob you already hosting the file in a web/HTTP storage area. See it right, but I’m sure that’s what you need.
– Thiago Lunardi
@Napoleon Menezes the answer solved his problem?
– Thiago Lunardi