Uploading files using .NET as client and PHP as Server

I was fed up with uploading the attachments using FTP as it was too slow (and vulnerable too), maintaining credentials to connect to server, managing permissions to specific folder etc. and all that nasty stuff.  Clients have also complained on this. After head-scratching and wall banging efforts, I planned to search for an alternative for FTP to upload the files. Found this way… Its pretty cool and uploading files much faster compared to FTP approach. Just wanted to share it with you, hoping that it may help someone who are in need!

This is a small tutorial which will teach you how to upload files using a C# client application to a server running PHP.

We’ll call the PHP Script “upload.php”, this is what it should contain:

<?php
$uploaddir = ‘upload/’; // Relative Upload Location of data file
if (is_uploaded_file($_FILES[‘file’][‘tmp_name’]))
{
$uploadfile = $uploaddir . basename($_FILES[‘file’][‘name’]);
echo “File “. $_FILES[‘file’][‘name’] .” uploaded successfully. “;
if (move_uploaded_file($_FILES[‘file’][‘tmp_name’], $uploadfile))
{
echo “File is valid, and was successfully moved. “;
}

else
print_r($_FILES);
}
else
{
echo “Upload Failed!!!”;
print_r($_FILES);
}
?>

and here is the C# code…

System.Net.WebClient Client = new System.Net.WebClient ();
Client.Headers.Add(“Content-Type”,”binary/octet-stream”);
byte[] result = Client.UploadFile (“http://your_server/upload.php&#8221;,”POST”,”C:\test.jpg”);
String s = System.Text.Encoding .UTF8 .GetString (result,0,result.Length );

In the C# part, replace “your_server.com” with your server, also notice that this is a test code, it will upload c:\\test.jpg, because I am testing with an image file, I am using the header “binary/octet-stream” as it works with all files (image, txt, etc)…

Hope this helps someone out there as I couldn’t find any tutorials about this specific matter on googling!

Opening default web browser from Windows Application

This post deals with opening default web browser from a .NET Application (esp. a Windows Application).

First, copy the following code and paste it in your class.

Private Declare Function ShellExecute _
                            Lib "shell32.dll" _
                            Alias "ShellExecuteA"( _
                            ByVal hwnd As Long, _
                            ByVal lpOperation As String, _
                            ByVal lpFile As String, _
                            ByVal lpParameters As String, _
                            ByVal lpDirectory As String, _
                            ByVal nShowCmd As Long) _
                            As Long

Then, you can use the function ShellExecute() as follows.

Private Sub Button1_Click()
   Dim objBrowser As Long
   objBrowser = ShellExecute(0, "open", "https://easyvsto.wordpress.com", 0, 0, 1)
End Sub

We can also open the default web browser as:

Process.Start("https://easyvsto.wordpress.com")

I usually create a Form and place a WebBrowser control in it and use it for opening any web pages. I will get back to you with that soon.

Microsoft reference for the topic, Click here

Deploying Office Solutions Using Windows Installer

Just found an awesome link from Microsoft`s MSDN site on Deploying VSTO solutions, enhancing setup project by adding features like installing prerequisites and other required stuff before the installation of add-in.

Here is the link

Common Pitfalls during PIA Deployment and Installation

Just found the following in Mary Lee`s blog. Keeping it here hoping that it would reach more people who are in need of it.

Deploying or installing the Office primary interop assemblies (PIAs) can sometimes be problematic. There are several common errors that occur when you try to deploy or install the PIAs.

Deployment pitfalls

A. Can I deploy the PIA DLL with my setup project? Yes, you can include the PIA DLL in your setup project, but we recommend that you use the Primary Interop Assemblies for the 2007 Microsoft Office system redistributable file from the Microsoft Download Center.  this way, the installer copies the PIAs into the GAC, and also installs binding redirects.  The binding redirects ensure that the latest version of the PIAs on the computer are always loaded.  This is how a solution developed for Microsoft Office 2003 still works on the 2007 Microsoft Office system.

Installation pitfalls

A. Do you have administrator privileges? The account used to install the PIAs must have administrator privileges.

B. Is the .NET Framework already installed? The computer must have a version of the .NET Framework installed (version 1.1, 2.0, 3.0, or 3.5) before the PIA installer is run. Windows XP does not include any version of the .NET Framework unless .NET 1.1 was downloaded from Windows Update. Windows Vista does include a version of the .NET Framework. When Microsoft Office is first installed, the .NET Framework installation is detected and the PIAs are automatically installed.

C. Is Business Contact Manager installed? If Outlook 2007 with Business Contact Manager is already installed on the computer, you may see the following error message when you install the Primary Interop Assemblies for the 2007 Microsoft Office system from the Microsoft Download Center:

Error 1316: Setup cannot read from file {PATH}\PIARedist.MSI. Check your connection to the network, or CD-ROM drive. For other potential solutions, see SETUP.CHM.

Business Contact Manager for Outlook 2007 installs the PIAs by default, and the file name is PIARedist.MSI.  However, the file from the Microsoft Download Center is called o2007pia.msi.  When you install the PIAs from the Microsoft Download Center, this name conflict causes the 1316 error.

The easiest thing to do here is to just ignore the error, because this message means that the PIAs are already installed. If you want to correct the error, uninstall the PIAs through the list of installed programs for the specific computer. Then you can reinstall the PIAs with the Primary Interop Assemblies for the 2007 Microsoft Office system from the Microsoft Download Center.

I don’t recommend that you follow the workaround posted at Error 1316 when installing Office 2007 PIA’s, which changes the name of the o2007pia.msi file to piaredist.msi and updates the product.xml to point to piaredist.msi.  If you change the name of the o2007pia.msi, anyone that installs the PIAs after you will experience the 1316 error.

If you discover other PIA pitfalls, please feel free to leave a comment or question so I can update this entry.  Thanks for reading!