Google Drive upload file from localhost hardisk, OOM issues due to HttpPostedFile - google-drive-api

This is web application, try to upload file in folder to Google Drive, I had done all the setting at Google console to get the credentials. It only work for smaller file, file size more than 200MB will hit OOM error. What should I do?
For the web config, I had set
<httpRuntime targetFramework="4.5" maxRequestLength="2000000000" executionTimeout="99999999" />
<gcAllowVeryLargeObjects enabled="true" />
After i get the Authorize from google at page load, i try to loop the directory to get the files.
protected string[] dirs = Directory.GetFiles(#"C:\Users\Yeep\Desktop\GoogleDrive");
After loop each of the file I will create FileStream / ReadAllBytes I had try 2 way.
foreach (string dir in dirs)
{
byte[] buffer = null;
using (FileStream fs = new FileStream(dir, FileMode.Open, FileAccess.Read))
{
buffer = new byte[fs.Length];
fs.Read(buffer, 0, (int)fs.Length);
}
//byte[] fileData = System.IO.File.ReadAllBytes(dir); //or this way
}
After loop through the file, to read all the bytes, I will run this / this.
Session["File"] = ConstructHttpPostedFile(buffer, fileName);
When Create an HttpRawUploadedContent instance it hit OOM, how to solve or got any other method?

Related

MySQL Connector/Net Bulk Uploader not finding file on remote server

I have pored through many pages trying to find an answer but have had no luck. I have a .NET page built in C# that has been working fine until a few days ago. Now it isn't working and I'm pulling my hair out to find out why.
The page has a file uploader that uploads a .csv file and saves it to a folder on the web server. Then it uses the MySQL Bulk Uploader to insert the records into the database on another server.
I have confirmed the file is uploading to the correct folder, but when MySQL tries to insert the records, it fails with the message "File 'E:\inetpub\wwwroot\training\data_uploads\filename.csv' not found (Errcode: 2 - No such file or directory)"
This page has worked for several years without any problem, but I updated some of the NuGet packages and removed some that were not being used, and now it's stopped working. What am I missing? Is there a package or a .dll I need to add back in? Unfortunately, I don't remember what I removed.
Here's the code I'm using:
protected void btnGo_Click(object sender, EventArgs e)
{
try
{
//if file is selected for upload
if (btnSelectFile.HasFile)
{
//upload data file to server
string path = string.Concat(Server.MapPath("~/data_uploads/" + btnSelectFile.FileName));
btnSelectFile.SaveAs(path);
string conString = ConfigurationManager.ConnectionStrings["nameOfConnectionString"].ConnectionString;
MySqlConnection conn = new MySqlConnection(conString);
conn.Open();
//get rid of old data
MySqlCommand truncateTerms = new MySqlCommand("TRUNCATE terms_temp;", conn);
truncateTerms.ExecuteNonQuery();
//create bulk uploader and set parameters
var bl = new MySqlBulkLoader(conn);
bl.TableName = "terms_temp";
bl.FieldTerminator = ",";
bl.FieldQuotationCharacter = '"';
bl.LineTerminator = "\r\n";
bl.FileName = path;
bl.NumberOfLinesToSkip = 2;
//insert data
var inserted = bl.Load(); //This is where it fails
conn.Close();
//do some other stuff
catch (Exception ex)
{
Label1.ForeColor = System.Drawing.Color.Red;
Label1.Text = ex.Message.ToString();
}
}
If you're bulk-loading a file that's stored on the web server, not the database server, you need to set MySqlBulkLoader.Local = true, to indicate that the file is local to the database client. Otherwise, the server will give an error that the file isn't found.
For security reasons you will also need to set AllowLoadLocalInfile=true in your connection string to enable this feature.

How to send a file greater than 25 MB using Java Mail API

I am designing an application which sends Email with attachments using Gmail's smtp host. But when the file is larger than 25 MB, then I get an error saying that "552-5.2.3 Your message exceeded Google's message size limits. Please visit https://support.google.com/mail/?p=MaxSizeError to view our size guidelines.
188sm2692677pfg.11 -gsmtp"
final String username = "username#gmail.com";
final String password = "password";
Properties prop = new Properties();
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.port", "587");
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(prop,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("username#gmail.com"));
message.setRecipients(
Message.RecipientType.TO,
InternetAddress.parse("receiver address"));
message.setSubject("This mail is a test mail");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Message");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
String filename = <Path>;
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
} catch (MessagingException e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
Is there any way of sending files greater than gmail's limit of 25 MB?
Can I change the file size upload limit from account settings or can I do something like any file will be uploaded as a drive link?
The user friendly way is probably to upload the file somewhere and add a link to the file in the mail.
It's also possible to split the file in several smaller parts and send each part in its own mail. The recipient then needs to join the files together again.
Zip-archivers can usually split large files into several zip-files that can then be join together again.
There's also raw splitting and joining. I haven't come across split commands built in to operating system distributions as standard. But your program could split the file in any way you desire.
Joining the files is then quite easy under Windows or Unix (Linux and others) operating systems. In Windows you go to the command prompt and use "copy": copy file1+file2+file3 finalfile In Unix you use "cat": cat file1 file2 file3 > finalfile
No. That is hard limit. Gmail itself states:
If your file is greater than 25 MB, Gmail automatically adds a Google Drive link in the email instead of including it as an attachment
That is what I would recommend for you as well, upload the file somewhere and paste a link in the mail. Options may be: Google Drive, Mega, Dropbox, S3, ...
Other than that there is nothing you can do.

Windows Phone 8 choose text file C#

i have a question. If there is a possibility at windows phone 8 at visual studio to create button event to read text file? i know about streamReader and if i declare wchich exacly file i want to read, but if i want to choose from list of files wchich i want to display. i did research on the Internet but i didint find an answer. I know i can use isolatedStorage to read music, video, image but not text files, on the app i created few files with text in it and i want users to have posibility to display one from this file, whichever they want to see. So, can you tell me how to do this?
You can use IsolatedStorage to read any file type you wish. You must of been using something like a Launcher that filters out the file type based on the Chooser.
You can open a file like this:
private async Task<string> ReadTextFile(string file_name)
{
// return buffer
string file_content = "";
// Get the local folder
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
if (local != null)
{
// Get the file
StorageFile file;
try
{
file = await local.GetFileAsync(file_name);
}
catch (Exception ex)
{
// no file, return empty
return file_content;
}
// Get the stream
System.IO.Stream file_stream = await file.OpenStreamForReadAsync();
// Read the data
using (StreamReader streamReader = new StreamReader(file_stream))
{
file_content = streamReader.ReadToEnd(); // read the full text file
streamReader.Close();
}
// Close the stream
file_stream.Close();
}
// return
return file_content;
}
If you want to get the PackageLocation (files that you added into the project like assets and images) then replace the LocalFolder with
Windows.ApplicationModel.Package package = Windows.ApplicationModel.Package.Current;
Windows.Storage.StorageFolder installedLocation = package.InstalledLocation;
With Windows Phone 8.1, File Pickers are allowed, consisting the same functionality you are expecting, so probably you might want to upgrade your app to WP8.1.
Here's more info on this API : Working with File Pickers

Windows phone 8 download then unzip to isolated storage

I'm writing a windows phone 8 application that have following functions
Download a zip file from the internet
Extract it to the isolated storage
I'm looking for a solution to deal with it but haven't found once. If you have any suggestion please help.
Thanks in advance!
EDIT:
I break it down into several steps:
Check if storage is available - DONE
Check if file is compressed - DONE
Use Background Transfer (or another method) to download to local folder and display information to user (percentage, ect.) - NOT YET
Unzip file to desired location in isolated storage - NOT YET
Do stuffs after that... - DONE
For step 4, I found and modified some script to extract file to isolated storage (using SharpGIS.UnZipper lib):
public async void UnzipAndSaveFiles(Stream stream, string name)
{
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var zipStream = new UnZipper(stream))
{
foreach (string file in zipStream.FileNamesInZip)
{
string fileName = Path.GetFileName(file);
if (!string.IsNullOrEmpty(fileName))
{
StorageFolder folder = ApplicationData.Current.LocalFolder;
folder = await folder.CreateFolderAsync("html", CreationCollisionOption.OpenIfExists);
StorageFile file1 = await folder.CreateFileAsync(name, CreationCollisionOption.ReplaceExisting);
//save file entry to storage
using (var writer = new StreamWriter(await file1.OpenStreamForWriteAsync()))
{
writer.Write(file);
}
}
}
}
}
}
This code is untested (since I haven't downloaded any file).
Can anyone point out any thing that should be corrected (enhanced)?
Can anyone help me to modify it to extract password-protected file (Obviously I have the key)?

Windows Phone 8 - Saving Microphone File as .wav

I am using following method to save a recording of microphone in WP8 to a file:
private void SaveToIsolatedStorage()
{
// first, we grab the current apps isolated storage handle
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
// we give our file a filename
string strSaveName = "myFile.wav";
// if that file exists...
if (isf.FileExists(strSaveName))
{
// then delete it
isf.DeleteFile(strSaveName);
}
// now we set up an isolated storage stream to point to store our data
IsolatedStorageFileStream isfStream =
new IsolatedStorageFileStream(strSaveName,
FileMode.Create, IsolatedStorageFile.GetUserStoreForApplication());
isfStream.Write(stream.ToArray(), 0, stream.ToArray().Length);
// ok, done with isolated storage... so close it
isfStream.Close();
}
The file is saved. However, I do not know where does it save it, and how can I access it.
I wish to permanently save it to the device so I can access it from outside the app (Let's say from a file explorer app, or from the music player app).
Thanks
Use this code to get saved file name from isolated storage and use this to read this file from stored loacation:
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
String[] filenames=myIsolatedStorage.GetFileNames();