miércoles, 22 de diciembre de 2010

Como descargar un archivo de una forma de un flujo (infopath)

Buenas, a continuación les dejo el código, para descargar un archivo, de una tarea de un flujo de sharepoin (Infopath), esto es para no tener que almacenarlo en alguna lista, o generarlo físicamente.

System.Collections.Hashtable extendedProp = Microsoft.SharePoint.Workflow.SPWorkflowTask.GetExtendedPropertiesAsHashtable(item);
//Response.ContentType = "text/HTML";
//Response.BinaryWrite(Convert.FromBase64String(extendedProp["FileAttached"] as String));

if (!String.IsNullOrEmpty((extendedProp["FileAttached"] as String)))
{
byte[] data = Convert.FromBase64String(extendedProp["FileAttached"] as String);
byte[] buffer = data;
Int32 blockSize = 10;

//get filename
int namebufferlen = data[20] * 2;
byte[] filenameBuffer = new byte[namebufferlen];
for (int i = 0; i < filenameBuffer.Length; i++)
{
filenameBuffer[i] = data[24 + i];
}

char[] asciiChars = UnicodeEncoding.Unicode.GetChars(filenameBuffer);

string filename = new string(asciiChars);

filename = filename.Substring(0, filename.Length - 1);

using (System.IO.Stream st = new System.IO.MemoryStream(data))
{
long dataLengthToRead = st.Length;
Response.ContentType = "text/plain"; //Or other you need
Response.AddHeader("Content-Disposition", String.Format("attachment; filename=\"{0}\"", filename));
//Response.AddHeader("Content-Disposition", "attachment; filename=\"" + theFileName + "\"");
while (dataLengthToRead > 0 && Response.IsClientConnected)
{
Int32 lengthRead = st.Read(buffer, 0, blockSize);
Response.OutputStream.Write(buffer, 0, lengthRead);
Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
Response.Flush();
Response.Close();
}
Response.End();
}


Saludos,