Writing to a new file in Azure App Service? - azure

I have a Spring Boot application running in an Azure App Service. The application uses FileUtils.writeStringToFile from org.apache.commons.io.FileUtils to write a string to a new file:
private String staticPath = "/static";
[...]
public void uploadFileContent(String content) throws Exception {
try {
File dir = new File(staticPath);
if (!dir.exists()) dir.mkdirs();
String relativePath = DateFormatUtils.format(new Date(), Constants.DateFormatTemp.YYYYMM_SPRIT);
String newFileName = String.valueOf(System.currentTimeMillis()) + UUID.randomUUID();
File fileDir = new File(dir, relativePath);
if (!fileDir.exists()) fileDir.mkdirs();
File newFile = new File(fileDir, newFileName + Constants.Split.SPOT + "csv");
FileUtils.writeStringToFile(newFile, content);
} catch (Exception e) {
throw e;
}
}
Locally it works, but if I run it in Azure then I get:
java.io.IOException: File 'example.csv' could not be created
The exception occurs at the last line of the try block, i.e. FileUtils.writeStringToFile(newFile, content);
Does writing to file system in Azure require certain special operations?

Related

Input output stream not working in Web Forms function

Can someone tell me why I keep getting a read and write timeout on this function? I have this as a code behind function on click even from a button. Everything as far as the data looks good until I get to the stream section and it still steps through, but when I check the Stream object contents after stepping into that object it states Read Timeout/Write Timeout: System invalid Operation Exception.
protected void SubmitToDB_Click(object sender, EventArgs e)
{
if (FileUploader.HasFile)
{
try
{
if (SectionDropDownList.SelectedValue != null)
{
if (TemplateDropDownList.SelectedValue != null)
{
// This gets the full file path on the client's machine ie: c:\test\myfile.txt
string strFilePath = FileUploader.PostedFile.FileName;
//use the System.IO Path.GetFileName method to get specifics about the file without needing to parse the path as a string
string strFileName = Path.GetFileName(strFilePath);
Int32 intFileSize = FileUploader.PostedFile.ContentLength;
string strContentType = FileUploader.PostedFile.ContentType;
//Convert the uploaded file to a byte stream to save to your database. This could be a database table field of type Image in SQL Server
Stream strmStream = FileUploader.PostedFile.InputStream;
Int32 intFileLength = (Int32)strmStream.Length;
byte[] bytUpfile = new byte[intFileLength + 1];
strmStream.Read(bytUpfile, 0, intFileLength);
strmStream.Close();
saveFileToDb(strFileName, intFileSize, strContentType, bytUpfile); // or use FileUploader.SaveAs(Server.MapPath(".") + "filename") to save to the server's filesystem.
lblUploadResult.Text = "Upload Success. File was uploaded and saved to the database.";
}
}
}
catch (Exception err)
{
lblUploadResult.Text = "The file was not updloaded because the following error happened: " + err.ToString();
}
}
else
{
lblUploadResult.Text = "No File Uploaded because none was selected.";
}
}
Try something like this:
using (var fileStream = FileUploader.PostedFile.InputStream)
{
using (var reader = new BinaryReader(fileStream))
{
byte[] bytUpfile = reader.ReadBytes((Int32)fileStream.Length);
// SAVE TO DB...
}
}

Getting Internal server error in file uploading?

I am new to linux environment.I have a web application which is running fine in windows environment and it is developed using play framework.Now we want to move our application from windows to linux. Here issue is,while uploading files i'm getting internal server error.I have given permissions as 777 for folder.Even the controller doesn't invoke the method,how to solve it?I am unable to find where the error goes,For all help thanks in advance.
Error is :
Controller action :
#helper.form(action =routes.HoForms.uploadHoFormsByHeadOffice(),'enctype -> "multipart/form-data" ,'id -> "shiftSummaryForm")
{
}
In routes file :
POST /HeadOfficeForms/upload controllers.HoForms.uploadHoFormsByHeadOffice()
Controller method :
public static Result uploadHoFormsByHeadOffice() throws Exception {
Logger.info("#C HoForms -->> uploadHoFormsByHeadOffice() -->> ");
}
Upload method() :
public static Result uploadHoFormsByHeadOffice() throws Exception {
final String basePath = "/var/www/html/pdfs/";
Date date = new Date();
DateFormat format = new SimpleDateFormat("dd-MM-yyyy");
String startDate = format.format(date);
date = format.parse(startDate);
play.mvc.Http.MultipartFormData body = request().body()
.asMultipartFormData();
String formType = body.asFormUrlEncoded().get("formType")[0];
FilePart upFile = body.getFile("hoFormFiles");
String fileName = upFile.getFilename();
try {
File ftemp = new File(basePath + "HeadOfficeForms/" + formType
+ "");
ftemp.mkdirs();
String filePathString = "HeadOfficeForms/" + formType + "/"
+ fileName;
File f1 = new File(ftemp.getAbsolutePath() + "/" + fileName);
File file = upFile.getFile();
file.setWritable(true);
file.setReadable(true);
f1.setWritable(true);
f1.setReadable(true);
Files.copy(file.toPath(), f1.toPath(), REPLACE_EXISTING);
HoForm.create(fileName, date, formType, filePathString);
flash("success", fileName + " Has been Successfully Uploaded");
} catch (IOException e) {
e.printStackTrace();
}
return redirect(routes.HoForms.showHoFormUploadPage());
}

cannot access the file because it is using by another program Error

I have been fighting with this issue since last week.still not able to solve.
i am sending file to client machine once i get in server automatically.but so many time when i handle file it throw an error that Cannot access the file because it is using by another program.
below my code
private static string SaveFileStream(string filePath, Stream stream, string Filename1)
{
string localresponse;
try
{
//this.SaveFileStream(System.Configuration.ConfigurationSettings.AppSettings[fileToPush.FileType].ToString() + "\\" + fileToPush.FileName, new MemoryStream(fileToPush.Content));
if (File.Exists(filePath))
{
File.Delete(filePath);
}
using (var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
stream.CopyTo(fileStream);
fileStream.Flush();
fileStream.Dispose();
}
FileInfo info = new FileInfo(filePath);
ExtractFile(info);
localresponse = "Successful";
}
catch (Exception ex)
{
localresponse = ex.Message;
}
return localresponse;
}

Android 6 get path to downloaded file

I our app (Xamarin C#) we download files from a server. At the end of a succeful download we get the URI to the newly-downloaded file and from the URI we get the file path:
Android.Net.Uri uri = downloadManager.GetUriForDownloadedFile(entry.Value);
path = u.EncodedPath;
In Android 4.4.2 and in Android 5 the uri and path look like this:
uri="file:///storage/emulated/0/Download/2.zip"
path = u.EncodedPath ="/storage/emulated/0/Download/2.zip"
We then use path to process the file.
The problem is that in Android 6 (on a real Nexus phone) we get a completely different uri and path:
uri="content://downloads/my_downloads/2802"
path="/my_downloads/2802"
This breaks my code by throwing a FileNotFound exception. Note that the downloaded file exists and is in the Downloads folder.
How can I use the URI I get from Android 6 to get the proper file path so I can to the file and process it?
Thank you,
donescamillo#gmail.com
I didn't get your actual requirement but it looks like you want to process file content. If so it can be done by reading the file content by using file descriptor of downloaded file. Code snippet as
ParcelFileDescriptor parcelFd = null;
try {
parcelFd = mDownloadManager.openDownloadedFile(downloadId);
FileInputStream fileInputStream = new FileInputStream(parcelFd.getFileDescriptor());
} catch (FileNotFoundException e) {
Log.w(TAG, "Error in opening file: " + e.getMessage(), e);
} finally {
if(parcelFd != null) {
try {
parcelFd.close();
} catch (IOException e) {
}
}
}
But I am also looking to move or delete that file after processing.
May you an build your URI with the download folder :
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toURI();
It's work. #2016.6.24
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals( action)) {
DownloadManager downloadManager = (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE);
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadId);
Cursor c = downloadManager.query(query);
if(c != null) {
if (c.moveToFirst()) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
String downloadFileUrl = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
startInstall(context, Uri.parse(downloadFileUrl));
}
}
c.close();
}
}
}
private boolean startInstall(Context context, Uri uri) {
if(!new File( uri.getPath()).exists()) {
System.out.println( " local file has been deleted! ");
return false;
}
Intent intent = new Intent();
intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction( Intent.ACTION_VIEW);
intent.setDataAndType( uri, "application/vnd.android.package-archive");
context.startActivity( intent);
return true;
}

windows azure upload file path error: 'Could not find file... '

I've been trying to upload a file to the Azure storage container.
When I run my web app locally, if file is not in the web site root I get an error:
Could not find file 'C:\Program Files (x86)\IIS Express\test.txt'.
If I copy the file to the web app root folder, it works fine.
Running web app on the cloud I get an error:
Could not find file 'D:\Windows\system32\test.txt'.
I can't get the full local file path from HttpPostedFileBase object.
The code:
private string UploadFile(HttpPostedFileBase file, string folder)
{
try
{
var date = DateTime.UtcNow.ToString("yyyyMMdd-hhmmss-");
var fileName = Path.GetFileName(file.FileName);
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=http;AccountName=test;AccountKey=asdfasfasdasdf");
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(folder);
bool b = container.CreateIfNotExists();
CloudBlockBlob blockBlob = container.GetBlockBlobReference(date + fileName);
blockBlob.Properties.ContentType = file.ContentType;
using (var fileStream = System.IO.File.OpenRead(fileName))
{
blockBlob.UploadFromStream(fileStream);
}
return blockBlob.Uri.AbsoluteUri;
}
catch (Exception ex)
{
return ex.Message;
}
The HttpPostedFileBase manual has this to say;
FileName Gets the fully qualified name of the file on the client.
That is, the file name is not one that can be opened on the server.
I think what you really want to do is use the InputStream property;
blockBlob.Properties.ContentType = file.ContentType;
blockBlob.UploadFromStream(file.InputStream); // Upload from InputStream
return blockBlob.Uri.AbsoluteUri;
Thank you so much: Joachim Isaksson (above) I spent the whole day yesterday fighting with this one line of code. I've voted your answer up but I thought I'd add a copy of your solution that lies within my own code now:
public async Task<ActionResult> UploadAsync()
{
try
{
HttpFileCollectionBase files = Request.Files;
int fileCount = files.Count;
if (fileCount > 0)
{
for (int i = 0; i < fileCount; i++)
{
CloudBlockBlob blob = blobContainer.GetBlockBlobReference(GetRandomBlobName(files[i].FileName));
blob.Properties.ContentType = files[i].ContentType;
blob.UploadFromStream(files[i].InputStream);
// the above 2 lines replace the following line from the downloaded example project:
//await blob.UploadFromFileAsync(Path.GetFullPath(files[i].FileName), FileMode.Open);
}
}
return RedirectToAction("Index");
}
}

Resources