Windows Live Writer Plugin – Uploading files

In this post, I will discuss how you can create a Windows Live Writer plugin, that allows a user to attach files to a blog post, when the user clicks publish the file will be uploaded via FTP, or directly using your blog provider if its supported.  Live Spaces, LiveJournal and Blogger do not support uploading files, in this case FTP will need to be setup via the Tools > Options > Accounts > Select and Edit > Images > Configure FTP.  This plugin will work the same as when inserting images onto a blog.

The plugin isn’t going to do anything special, I’m sure you will be able to use the other posts i’ve wrote to create something special.

To have access to adding files to the blog, your plugin will need to inherit from SmartContentSource and override CreateContent, CreateEditor and GeneratePublishHtml.  On this post, I’m only going to implement GeneratePublishHtml functionality.

 Below is all the code required to add a file to the blog. 

public override string GeneratePublishHtml(ISmartContent content, IPublishingContext publishingContext)
{
    string filename = “ZipTest.zip”;
    string fullpath = “F:ZipTest.zip”;
    content.Files.Add(filename, fullpath);
    string filePath = content.Files.GetUri(content.Files.Filenames[0]).ToString();
    return string.Format(“

Download File – {1}

“, filePath, filename);
}

content.Files.Add takes a filename and a full file path to the file which you wish to upload.  When you add the file, LiveWriter copies the file to a temporary location, by using content.Files.GetUri() we can gain access to this temp file path.

We then return the html code to insert a download link to the file into the post. 

There is a problem, when inserting the link into the blog, the link is to your local directory – not very useful on your blog.  If you just return plain text from GeneratePublishHtml then LiveWriter will just post the local path text.  However, if you include the URL within a link tag then it replaces the path with a $ (It will become

Download File – ZipTest.zip

). 

When you click publish, the file will be uploaded to the web server and the $ will be replaced with the path to the file on the web server.  One problem with this is that it uses the path set in the options dialog, which is used for the images.  If you upload any files, they will be put in the same directory as your images.  It would have been great to be able to set which directory the files ended up in – maybe a separate dialog setting – or just use a directory called Files instead of Images.

Download – LiveWriterUploader.cs.txt

Technorati tags:

One thought on “Windows Live Writer Plugin – Uploading files”

  1. Hi Ben Hall, I was interested in writing plugin for WLW so much. After some first plugin, I stuck in a problem how to upload a file using the built-in function/plugin of WLW. I searched around internet but cannot find anything until I saw your blog. Yeh, this entry was very helpful. Thank so much!

Leave a Reply

Your email address will not be published. Required fields are marked *