Saturday, October 22, 2011

ViewState HttpPostedFile

When I was trying to implement the functionality of uploading file I looked into putting HttpPostedFile into ViewState -

FileUpload.PostedAs is a HttpPostedFile

BAD idea. - There is no constructor for HttpPostedFile.


SO DO NOT GO THAT PATH!! You can cache the file content. Check the blog


Friday, October 21, 2011

File upload with two different ways!!!

In recent project I had to make two different types of FileUploads work -
1. Asp.net fileupload a
2. Ajax - AsyncFileupload

Both implementations were not that hard. Complete description is as follows:

Asp.net FileUpload

Visible="false" />
ValidationExpression="^(([a-zA-Z]:)|(\\{1}\w+)\$?)(\\(\w.*))+(.xls|.xlsx)$" ControlToValidate="fuPricingAttachment">


In above example - is the control used and the regex is for informing that only .xls and .xlsx types of files can be uploaded. ValidationGroup allows to validate the content when 'submit' button is clicked.

I did create -

public string pricingFileGuid
{
get
{
if (ViewState["strPricingFileGuid"] != null)
return ViewState["strPricingFileGuid"].ToString();
else
return string.Empty;
}
set { ViewState["strPricingFileGuid"] = value; }
}

so that I can cache the file content with key of GUID.

Core piece of storing file content is - this way content does not get stored until user decides to save the file content in the database by hitting 'Save' or 'Submit'

var fileKey = Guid.NewGuid();
if (!string.IsNullOrEmpty(fileName))
{
var fileStream = new Byte[fuPricingAttachment.PostedFile.ContentLength];
fuPricingAttachment.PostedFile.InputStream.Read(fileStream, 0, fuPricingAttachment.PostedFile.ContentLength);
Cache[fileKey.ToString()] = fileStream;
this.pricingFileGuid = fileKey.ToString();
}

The code which Saves file to file share and to database :

if (!string.IsNullOrEmpty(this.generalFileGuid))
{
var fileStream = new Byte[((Byte[])Cache[this.generalFileGuid]).Length];

try
{
// Open file for reading
System.IO.FileStream _FileStream = new System.IO.FileStream(lnkGeneralAttachment.Text, System.IO.FileMode.Create, System.IO.FileAccess.Write);
// Writes a block of bytes to this stream using data from a byte array.
_FileStream.Write(fileStream, 0, fileStream.Length);
// close file stream
_FileStream.Close();

}
catch (Exception _Exception)
{
throw new Exception("Error while uploading file.");
}
}

At any point if you have any questions/comments or you want full code of pages let me know and I might be able to provide it to you within no time.

I will upload AsyncFileUpload code tomorrow :) happy coding!



Wednesday, September 21, 2011

Custom project templates

Easy pizy creation of project template
http://saraford.net/2008/10/16/did-you-know-you-can-create-project-templates-336/

Custom project templates for C# can be compiled into a zip file and you just need to copy to the and visual studio should pick up the project templates.

C:\Users\[your name]\Documents\Visual Studio 2010\Templates\ProjectTemplates\Visual C#

Looks like there are several ways to create project templates - zip file creation seems easiest.

Monday, August 29, 2011

The project file could not be loaded. Root element is missing.


Loading \....vbproj ...

error : Unable to read the project file 'WebSite.vbproj'.

..\..\...\..\...vbproj.user: The project file could not be loaded. Root element is missing.

File size of vbproj.user was found to 0 bytes. Deleting one of the vbproj.user fixed the problem. This is just FYI, in case somebody else come across this in future


Feel free to post your questions or comments.