0

I am creating a Sharepoint2010 webpart page which downloads documents from a webservice (So the documents do not get saved in Sharepoint2010).

I have a windows application which does exactly the same. The windows application executes when the download button is clicked.

My Code

private void downloadButton_Click( object sender, EventArgs e )
const int BUFFER_SIZE = 65535;
if ( saveFileDialog.ShowDialog() == DialogResult.OK )
{
    //Executes Webservice code to retrieve document.
    const int BUFFER_SIZE = 65535;
    DataTable dataSource = (DataTable)this.Page.Session[SESSION_ID];
    DataRow[] rows = dataSource.Select(string.Format("ID = {0}", "'"+ID+"'"));
    FileClient _fileClient = new FileClient();
    Stream checkOut = _fileClient.RetrieveDocument(Ticket, ID);
    FileStream outStream = new FileStream("FILELOCATION", FileMode.Create, FileAccess.Write, FileShare.Read, BUFFER_SIZE);
    byte[] buffer = new byte[BUFFER_SIZE];
    int bytesread = 0;
    while ((bytesread = checkOut.Read(buffer, 0, BUFFER_SIZE)) > 0)
    {
        outStream.Write(buffer, 0, bytesread);
    }
}

if i use this code in sharepoint2010:

if( saveFileDialog.ShowDialog() == DialogResult.OK )

it throws the following exception:

Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDeskOnly style to display a notification from a service application.

Searching on google i got the answer that windows forms cannot be used on a sharepoint site. So how can i do the same as this form in Sharepoint 2010?

EDIT After more testing and using Arsalan Adam Khatri's comment (Thank you btw) I created above code. The only variable missing is a File location where the downloaded file shoulds be saved on the client. How do you create a popup where the user selects the filelocation and how do you get this data and how do you set it so the webpart can use it?

EDIT 2 I entered a hardcoded savinglocation for testing purposes. When testing the sharepoint site and downloading a file. It downloads the file on the Server Machine using the hard coded file location instead of the client machine.

1 Answer 1

1

AFAIK Save File Dialog, Open File Dialog are form controls and not web controls... So they will not work in Web environment!

You can use normal File Download code to let the user download the file and select where to save, see below links:
Download a file into client with ASP.NET 2.0
File Download in ASP.NET C#

Once you have the byte[] of the file, write it on Response.WriteStream/Response.OutputStream.Write... Make sure you add correct header and content type [Response.ContentType]

I hope this helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.