Reputation: 101
I'm uploading documents to a temp folder using the Upload Control FileUploadComplete Event, but I'm trying to monitor what is uploaded in the temp folder so I can then move it into the real application folder when the submit button is clicked.
I'll upload what code I have; hopefully some of it is useful, Thanks
//global list to store temp uploads
protected List<string> listTempImages = new List<string>();
protected void Page_Load(object sender, EventArgs e)
{
//.....
}
//upload to temp method
protected void ucUploadFile_FileUploadComplete(object sender, FileUploadCompleteEventArgs e)
{
e.CallbackData = SavePostedFiles(e.UploadedFile);
}
string SavePostedFiles(UploadedFile uploadedFile)
{
if (!uploadedFile.IsValid)
{
return string.Empty;
}
string strAttachmentFolderQuery = "CALL storedProcedure"; //
DataTable dtFolderLocation = new DataTable();
BasicUtilities.SelectCommand(ref strAttachmentFolderQuery, ref dtFolderLocation, sqlConn, true);
string strFolderLocation = @dtFolderLocation.Rows[0]["ColumnName"].ToString();
string strCallFolder = strFolderLocation + @"Temp\\";
/** File doesn't exists, create folder and copy in new file */
listTempImages.AddRange(uploadedFile.FileName.Split('.'));
string strFileName = listTempImages[0];
FileInfo fi = new FileInfo(strFileName);
String timeStamp;
timeStamp = DateTime.Now.ToString("yyyMMddhhmmss");
string fullFileName = MapPath(strFolderLocation) + fi.Name;
ucUploadFile.SaveAs(strCallFolder + fi.Name + "_" + timeStamp + "." + listTempImages[1]);
//file_name_uploadTimeStamp.extention
string fileLabel = fi.Name;
string fileLength = uploadedFile.FileContent.Length / 1024 + "k";
/** adding image to temp to ensure you can get it or delete it */
listTempImages.Add(strCallFolder + fi.Name + "_" + timeStamp);
return string.Format("{0} ({1})|{2}", fileLabel, fileLength, fi.Name);
}
protected void btnSubmit_Click1(object sender, EventArgs e)
{
if (listTempImages != null)
{
/** Upload attachment to server */
while (listTempImages.Count >0)
{
Upload(callID, reporterID);
}
}
else
{
/** Upload panel doesn't have have a file */
}
}
**EDIT
<dx:ASPxButton ID="btnSubmit" runat="server" Text="Submit"
ClientIDMode="AutoID"
onclick="btnSubmit_Click1" AutoPostBack="False">
<ClientSideEvents Click="function(s, e) {
if(document.getElementById('ucUploadFile_Input_0') == null){
e.processonserver = false;
$('#ucUploadFile_Add').click();
e.processonserver = true;
}
}" />
</dx:ASPxButton>
Upvotes: 3
Views: 822
Reputation: 1999
As Adil says HTTP is stateless. every time request goes to the server you get a refresh page.
If viewstate is not doing your work you can store your data in session. But avoid saving large amount of data in session or viewstate. if you have large data to store then use xml to store data.some of the benifite of using xml are
- very light
- fastest approach to travel data
- licence independent
here are some ink for state management refer to state management and State Management in ASP.NET
Upvotes: 1
Reputation: 148110
HTTP is stateless protocol and the global variable are not preserved between postback. You can store data required to be preserved in postback
in ViewState. If the data is not small it would be better for storing it in some persistent medium like XML / database to keep the ViewState as small as possible as it is added to page size ultimately.
ViewState["listTempImages"] = listTempImages;
Later access the object by getting from ViewState
listTempImages = ViewState["listTempImages"] as List<string>;
In computing, a stateless protocol is a communications protocol that treats each request as an independent transaction that is unrelated to any previous request so that the communication consists of independent pairs of requests and responses. A stateless protocol does not require the server to retain session information or status about each communications partner for the duration of multiple requests. In contrast, a protocol which requires the keeping of internal state is known as a stateful protocol. To read more about state less see this wikipedia article
View state is the method that the ASP.NET page framework uses to preserve page and control values between round trips. When the HTML markup for the page is rendered, the current state of the page and values that must be retained during postback are serialized into base64-encoded strings. This information is then put into the view state hidden field or fields, MSDN
Upvotes: 2