Transfer file from Windows Mobile device to...anywhere - c#

I can't seem to find a solution to this issue. I'm trying to get my Compact Framework application on Windows Mobile 6 to have the ability to move a file on its local filesystem to another system.
Here's the solutions I'm aware of:
FTP - Problem with that is most of
the APIs are way to expensive to use.
HTTP PUT - As far as I have been able to find, I can't use anonymous PUT with IIS7, and that's the web server the system is running. (An extreme workaround for this would be to use a different web server to PUT the file, and have that other system transfer it to the IIS system).
Windows share - I would need authentication on the shares, and I haven't seen that a way to pass this authentication through windows mobile.
The last resort would be to require that the devices be cradled to transfer these files, but I'd really like to be able to have these files be transferred wirelessly.

FTP: define "too expensive". Do you mean performance or byte overhead or dollar cost? Here's a free one with source.
HTTP: IIS7 certainly supports hosting web services or custom IHttpHandlers. You could use either for a data upload pretty easily.
A Windows Share simply requires that you to P/Invoke the WNet APIs to map the share, but it's not terribly complex.

I ended up just passing information to a web server via a PHP script.
The options provided above just didn't work out for my situation.
Here's the gist of it. I've got some code in there with progress bars and various checks and handlers unrelated to simply sending a file, but I'm sure you can pick through it. I've removed my authentication code from both the C# and the PHP, but it shouldn't be too hard to roll your own, if necessary.
in C#:
/*
* Here's the short+sweet about how I'm doing this
* 1) Copy the file from mobile device to web server by querying PHP script with paramaters for each line
* 2) PHP script checks 1) If we got the whole data file 2) If this is a duplicate data file
* 3) If it is a duplicate, or we didn't get the whole thing, it goes away. The mobile
* device will hang on to it's data file in the first case (if it's duplicate it deletes it)
* to be tried again later
* 4) The server will then process the data files using a scheduled task/cron job at an appropriate time
*/
private void process_attempts()
{
Uri CheckUrl = new Uri("http://path/to/php/script?action=check");
WebRequest checkReq = WebRequest.Create(CheckUrl);
try
{
WebResponse CheckResp = checkReq.GetResponse();
CheckResp.Close();
}
catch
{
MessageBox.Show("Error! Connection not available. Please make sure you are online.");
this.Invoke(new Close(closeme));
}
StreamReader dataReader = File.OpenText(datafile);
String line = null;
line = dataReader.ReadLine();
while (line != null)
{
Uri Url = new Uri("http://path/to/php/script?action=process&line=" + line);
WebRequest WebReq = WebRequest.Create(Url);
try
{
WebResponse Resp = WebReq.GetResponse();
Resp.Close();
}
catch
{
MessageBox.Show("Error! Connection not available. Please make sure you are online.");
this.Invoke(new Close(closeme));
return;
}
try
{
process_bar.Invoke(new SetInt(SetBarValue), new object[] { processed });
}
catch { }
process_num.Invoke(new SetString(SetNumValue), new object[] { processed + "/" + attempts });
processed++;
line = dataReader.ReadLine();
}
dataReader.Close();
Uri Url2 = new Uri("http://path/to/php/script?action=finalize&lines=" + attempts);
Boolean finalized = false;
WebRequest WebReq2 = WebRequest.Create(Url2);
try
{
WebResponse Resp = WebReq2.GetResponse();
Resp.Close();
finalized = true;
}
catch
{
MessageBox.Show("Error! Connection not available. Please make sure you are online.");
this.Invoke(new Close(closeme));
finalized = false;
}
MessageBox.Show("Done!");
this.Invoke(new Close(closeme));
}
In PHP (thoroughly commented for your benefit!):
<?php
//Get the GET'd values from the C#
//The current line being processed
$line = $_GET['line'];
//Which action we are doing
$action = $_GET['action'];
//# of lines in the source file
$totalLines = $_GET['lines'];
//If we are processing the line, open the data file, and append this new line and a newline.
if($action == "process"){
$dataFile = "tempdata/SOME_KIND_OF_UNIQUE_FILENAME.dat";
//open the file
$fh = fopen($dataFile, 'a');
//Write the line, and a newline to the file
fwrite($fh, $line."\r\n");
//Close the file
fclose($fh);
//Exit the script
exit();
}
//If we are done processing the original file from the C# application, make sure the number of lines in the new file matches that in the
//file we are transferring. An expansion of this could be to compare some kind of hash function value of both files...
if($action == "finalize"){
$dataFile = "tempdata/SOME_KIND_OF_UNIQUE_FILENAME.dat";
//Count the number of lines in the new file
$lines = count(file($dataFile));
//If the new file and the old file have the same number of lines...
if($lines == $totalLines){
//File has the matching number of lines, good enough for me over TCP.
//We should move or rename this file.
}else{
//File does NOT have the same number of lines as the source file.
}
exit();
}
if($action == "check"){
//If a file with this unique file name already exists, delete it.
$dataFile = "tempdata/SOME_KIND_OF_UNIQUE_FILENAME.dat";
if(file_exists($dataFile)){
unlink($dataFile);
}
}
?>

Related

Monitor FTP directory in ASP.NET/C#

I have FileSystem watcher for a local directory. It's working fine. I want same to implement for FTP. Is there any way I can achieve it? I have checked many solutions but it's not clear.
Logic: Want to get files from FTP later than some timestamp.
Problem faced: Getting all files from FTP and then filtering the result is hitting the performance (used FtpWebRequest).
Is there any right way to do this? (WinSCP is on hold. Cant use it now.)
FileSystemWatcher oFsWatcher = new FileSystemWatcher();
OFSWatchers.Add(oFsWatcher);
oFsWatcher.Path = sFilePath;
oFsWatcher.Filter = string.IsNullOrWhiteSpace(sFileFilter) ? "*.*" : sFileFilter;
oFsWatcher.NotifyFilter = NotifyFilters.FileName;
oFsWatcher.EnableRaisingEvents = true;
oFsWatcher.IncludeSubdirectories = bIncludeSubdirectories;
oFsWatcher.Created += new FileSystemEventHandler(OFsWatcher_Created);
You cannot use the FileSystemWatcher or any other way, because the FTP protocol does not have any API to notify a client about changes in the remote directory.
All you can do is to periodically iterate the remote tree and find changes.
It's actually rather easy to implement, if you use an FTP client library that supports recursive listing of a remote tree. Unfortunately, the built-in .NET FTP client, the FtpWebRequest does not. But for example with WinSCP .NET assembly, you can use the Session.EnumerateRemoteFiles method.
See the article Watching for changes in SFTP/FTP server:
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = "example.com",
UserName = "user",
Password = "password",
};
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
List<string> prevFiles = null;
while (true)
{
// Collect file list
List<string> files =
session.EnumerateRemoteFiles(
"/remote/path", "*.*", EnumerationOptions.AllDirectories)
.Select(fileInfo => fileInfo.FullName)
.ToList();
if (prevFiles == null)
{
// In the first round, just print number of files found
Console.WriteLine("Found {0} files", files.Count);
}
else
{
// Then look for differences against the previous list
IEnumerable<string> added = files.Except(prevFiles);
if (added.Any())
{
Console.WriteLine("Added files:");
foreach (string path in added)
{
Console.WriteLine(path);
}
}
IEnumerable<string> removed = prevFiles.Except(files);
if (removed.Any())
{
Console.WriteLine("Removed files:");
foreach (string path in removed)
{
Console.WriteLine(path);
}
}
}
prevFiles = files;
Console.WriteLine("Sleeping 10s...");
Thread.Sleep(10000);
}
}
(I'm the author of WinSCP)
Though, if you actually want to just download the changes, it's a way easier. Just use the Session.SynchronizeDirectories in the loop.
while (true)
{
SynchronizationResult result =
session.SynchronizeDirectories(
SynchronizationMode.Local, "/remote/path", #"C:\local\path", true);
result.Check();
// You can inspect result.Downloads for a list for updated files
Console.WriteLine("Sleeping 10s...");
Thread.Sleep(10000);
}
This will update even modified files, not only new files.
Though using WinSCP .NET assembly from a web application might be problematic. If you do not want to use a 3rd party library, you have to do with limitations of the FtpWebRequest. For an example how to recursively list a remote directory tree with the FtpWebRequest, see my answer to List names of files in FTP directory and its subdirectories.
You have edited your question to say that you have performance problems with the solutions I've suggested. Though you have already asked a new question that covers this:
Get FTP file details based on datetime in C#
Unless you have access to the OS which hosts the service; it will be a bit harder.
FileSystemWatcher places a hook on the filesystem, which will notify your application as soon as something happened.
FTP command specifications does not have such a hook. Besides that it's always initiated by the client.
Therefor, to implement such logic you should periodical perform a NLST to list the FTP-directory contents and track the changes (or hashes, perhaps (MDTM)) yourself.
More info:
FTP return codes
FTP
I have got an alternative solution to do my functionality.
Explanation:
I am downloading the files from FTP (Read permission reqd.) with same folder structure.
So everytime the job/service runs I can check into the physical path same file(Full Path) exists or not If not exists then it can be consider as a new file. And Ii can do some action for the same and download as well.
Its just an alternative solution.
Code Changes:
private static void GetFiles()
{
using (FtpClient conn = new FtpClient())
{
string ftpPath = "ftp://myftp/";
string downloadFileName = #"C:\temp\FTPTest\";
downloadFileName += "\\";
conn.Host = ftpPath;
//conn.Credentials = new NetworkCredential("ftptest", "ftptest");
conn.Connect();
//Get all directories
foreach (FtpListItem item in conn.GetListing(conn.GetWorkingDirectory(),
FtpListOption.Modify | FtpListOption.Recursive))
{
// if this is a file
if (item.Type == FtpFileSystemObjectType.File)
{
string localFilePath = downloadFileName + item.FullName;
//Only newly created files will be downloaded.
if (!File.Exists(localFilePath))
{
conn.DownloadFile(localFilePath, item.FullName);
//Do any action here.
Console.WriteLine(item.FullName);
}
}
}
}
}

Vision API C# - reading stored image's URL in Azure

//If the user uploaded an image, read it, and send it to the Vision API
if (activity.Attachments.Any() && activity.Attachments.First().ContentType.Contains("image"))
{
//stores image url (parsed from attachment or mess`enter code here`age)
string uploadedImageUrl = activity.Attachments.First().ContentUrl; ;
uploadedImageUrl = HttpUtility.UrlDecode(uploadedImageUrl.Substring(uploadedImageUrl.IndexOf("file=") + 5));
using (Stream imageFileStream = File.OpenRead(uploadedImageUrl))
{
try
{
analysisResult = await visionClient.AnalyzeImageAsync(imageFileStream, visualFeatures);
}
catch (Exception e)
{
analysisResult = null; //on error, reset analysis result to null
}
}
}
//Else, if the user did not upload an image, determine if the message contains a url, and send it to the Vision API
else
{
try
{
analysisResult = await visionClient.AnalyzeImageAsync(activity.Text, visualFeatures);
}
catch (Exception e)
{
analysisResult = null; //on error, reset analysis result to null
}
}
I am trying the code above. I got the code from here: https://docs.botframework.com/en-us/bot-intelligence/vision/#example-vision-bot.
As is, the code does what it can do when ran locally, but reading the image's URL from an uploaded file doesn't work after I published my bot to Azure and ran it from there.
I attempted to debug by attaching Visual Studio directly to my published webapp bot in Azure. It looked like the webapp is unable read the stored image's URL from the Azure server temp storage location or can't access the temp storage location.
This line:
uploadedImageUrl = HttpUtility.UrlDecode(uploadedImageUrl.Substring(uploadedImageUrl.IndexOf("file=") + 5));
Shows this value:
"https://bcattachmentsprod.blob.core.windows.net/at4984/Gv4cOx6OdSl- original"
Then, this line:
using (Stream imageFileStream = File.OpenRead(uploadedImageUrl))
Changes the value to:
"s://bcattachmentsprod.blob.core.windows.net/at4984/Gv4cOx6OdSl-original"
And then it just stops.
Has anyone ran into an issue like this? How do I go about solving this issue?
Thanks!
If you have a image URL, you should simply call AnalyzeImageAsync with that URL instead of creating a stream as you have. The File class should be used for files on your local drive/network. By creating a stream, you download the image once to your VM, then upload it to the vision service, doubling the work.

downloading file error

I wrote a program to download files from a website by using WebClient.DownloadFile().
public static void downWeb()
{
WebClient myWebClient = new WebClient();
path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
if (add() == 1)
{
Console.WriteLine("Response is " + add());
Console.WriteLine("Downloading File = " + dynFileName + "....");
myWebClient.DownloadFile(fullAddress, (path + dynFileName));
}
}
public static int add()
{
string url = fullAddress;
WebRequest webRequest = WebRequest.Create(url);
WebResponse webResponse;
try
{
webResponse = webRequest.GetResponse();
}
catch
{
return 0;
}
return 1;
}
downWeb() is a function to be called in the Main() function.
add() is a function that tests the availability of the file on server. If response is positive, it returns value "1".
fullAddress = address from where the files has to downloaded. It's changing every time before calling this function in a loop present in Main().
When I start my application, I ask the user to:
1) Enter URL to be downloaded i.e. www.1234.com\samplefiles\pg-1.pdf
2) Number of pages to be downloaded (By changing the above filename no. in a loop as rest of the url is same on server)
Now my problem is when I am downloading files, first file downloads PERFECTLY, but the second download is never finished. It says "REQUEST TIMED OUT", and my application closes.
I don't know what's happening here.
How can this be solved?
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse.aspx
You must call the Close method to close the stream and release the connection. Failure to do so may cause your application to run out of connections.
Your problem likely is related to the fact that you do not dispose of your connections. You should make sure that you don't leak them.

In ASP.NET, What could cause image manipulation to fail when using Fx or chrome, but succeed when using IE?

I am in a rather unusual pickle. I am modifying an image uploader and I thought I had it working. It needed to:
take a file from the client and upload it to server.
If file is an image, perform resizing operations on it.
If file is an image, create a thumbnail.
What I have works great when uploading images with Internet Explorer 8. But, when I upload images using Chrome, or Firefox3.+, the image gets uploaded but steps 2 and 3 are not performed. I don't get any server errors or anything. As 2 and 3 are steps that are performed on the server I have no idea how a change in browser could effect them.
I'm not sure if it has anything to do with my checking for whether the file is an image or not. But, for the sake of being thorough, here's the code I use:
try
{
string Filename = FileSystemUtilities.CleanupFilename(Path.GetFileName(hpf.FileName));
Filename = hpf.FileName;
string FileToSave = DestDir + Path.DirectorySeparatorChar + Path.GetFileName(Filename);
hpf.SaveAs(FileToSave);
bool IsImageFileType = ImageUtilities.IsImage(Filename, imageExtensions);
// below does not seem to execute when using non ie browser
// everything is smooth sailing when using ie.
if (IsImageFileType)
{
ImageUtilities.ResizeImageIfNecessary(FileToSave, mainMaxWidth, mainMaxHeight);
ImageUtilities.CreateThumbnail(FileToSave, thumbMaxWidth, thumbMaxHeight);
}
ValidOperation++;
sb.AppendFormat("{0} uploaded successfully<br/>", Filename);
}
Any thoughts? Why would server side code behave differently based on browser?
Edit: ImageUtilities.IsImage()
public static bool IsImage(string file, string[] imageExtensions)
{
file = Path.GetFullPath(file);
if (File.Exists(file))
{
string CurrentFileExtension = Path.GetExtension(file);
return imageExtensions.Count(x => x == CurrentFileExtension) > 0 ? true : false;
}
else
{
return false; //file doesn't exist
}
}
This difference would be caused by a difference in the filename sent by the browsers.
For example, some browsers include the full path.
Your ImageUtilities.IsImage function can't handle the filename sent by non-IE browsers.
EDIT: Your function is very wrong.
Change it to
return imageExtensions.Contains(Path.GetExtension(file),
StringComparer.OrdinalIgnoreCase);

Multiple Download/Upload parallely in FTP using C#

I want to do Multiple Download/Upload parallely in FTP using C# without using FTPWebRequest.
I have written my custom code and when i try to download two files simultaneously first one get download properly while second one shows size as 0 KB(it also downloads).
public void sendCommand(String command, params string[] strfilename)
{
if (command == "RETR ") //Downloading file from Server
{
FileStream output = null;
if (!File.Exists(strfilename[0]))
output = File.Create(strfilename[0]);
else
output = new FileStream(strfilename[0] , FileMode.Open);
command = "RETR " + strfilename[0];
Byte[] cmdBytes = Encoding.ASCII.GetBytes((command + "\r\n").ToCharArray());
clientSocket.Send(cmdBytes, cmdBytes.Length, 0);
Socket csocket = createDataSocket();
DateTime timeout = DateTime.Now.AddSeconds(this.timeoutSeconds);
while (timeout > DateTime.Now)
{
this.bytes = csocket.Receive(buffer, buffer.Length, 0);
output.Write(this.buffer, 0, this.bytes);
if (this.bytes <= 0)
{
break;
}
}
// this.BinaryMode = true;
output.Close();
if (csocket.Connected) csocket.Close();
this.readResponse();
MessageBox.Show("File Downloaded successfully");
else if....so on
}
}
In my main method i do like this:
ftpcommand.sendCommand("RETR ","RMSViewer.xml"); //Downloading from Server
ftpcommand.sendCommand("RETR ","cms.xml");//Downloading from Server
Any code snippet....
As Dave said, you'd need separate instances of your ftpCommand class. Look into use the BackgroundWorker to run the commands in the background (asynchronously).
How are you issuing your requests simultaneously?
Threaded?
If so - you may want to ensure you're creating separate instances of your "ftpcommand" class.
I think we'll need more information to be able to help you :)

Categories