This project is read-only.

Response is not available in this context

Nov 12, 2008 at 1:17 PM
Edited Nov 13, 2008 at 5:13 AM
I am trying to upload a document to Scribd. In the form I have a File Upload Control, a Textbox, a label and a button. Selecting the file and clicking on upload should upload the file to Scribd.
"Scribd.Net.Document.Upload(filename, accessType);" gives the error as "Response is not available in this context"

Code for default.aspx.cs is as below.

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Scribd.Net;

public partial class _Default : System.Web.UI.Page
{
    private string _title = "";
    private EventHandler<UserEventArgs> _loggedInHandler;
    protected void Page_Load(object sender, EventArgs e)
    {
        LoadLoginSettings();
        InitializeScribdEventHandlers();
        _loggedInHandler = new EventHandler<UserEventArgs>(User_LoggedIn);
        this.TextBox1.Visible = false;
    }
    private void InitializeScribdEventHandlers()
    {
        Document.Uploaded += new EventHandler<DocumentEventArgs>(Document_Uploaded);
        Document.Saved += new EventHandler<DocumentEventArgs>(Document_Saved);
        Document.UploadProgressChanged += new EventHandler<System.Net.UploadProgressChangedEventArgs>(Document_UploadProgressChanged);
        Service.Error += new EventHandler<ScribdEventArgs>(Service_Error);
    }
    private void ShowError(string msg)
    {
         this.Label1.Text = msg;
    }

    // Scribd Error Handler
    void Service_Error(object sender, ScribdEventArgs e)
    {
        ShowError("Scribd Error: " + e.Message);
    }

    private void LoadLoginSettings()
    {
        try
        {
            Service.APIKey = "XXXXXXXXXX";
            Service.SecretKey = "XXXXXXXXXX";
            Service.EnforceSigning = true;
            Service.PublisherID = "XXXXXXXXXX";  // Optional
            LoginUser("XXXXXXX", "XXXXXXXXXX");
        }
        catch (Exception)
        {
        }
    }

    void LoginUser(string user, string password)
    {
        // Subscribe to events
        Scribd.Net.User.LoggedIn += _loggedInHandler;
        Scribd.Net.User.LoginFailed += _loggedInHandler;

        // Sign into the service
        bool signInResult = Scribd.Net.User.Login(user, password);
        if (signInResult)
        {
            this.Label1.Text = "1";
        }
        else
        {
            this.Label1.Text = "0";
        }
    }

    void User_LoggedIn(object sender, UserEventArgs e)
    {
        Scribd.Net.User.LoggedIn -= _loggedInHandler;
        Scribd.Net.User.LoginFailed -= _loggedInHandler;
        if (e.Success)
        {
            
        }
        else
        {
            
        }
    }

    protected void cmdUpload_Click(object sender, EventArgs e)
    {
        String fName = this.FileUpload1.PostedFile.FileName;
        String fExt = System.IO.Path.GetExtension(fName);
        _title = System.IO.Path.GetFileNameWithoutExtension(fName);
        try
        {
            UploadDocumentToScribd(fName, true);
        }
        catch (Exception Ex)
       {
           this.Label1.Text = Ex.Message;
       }
    }
    private void PrepareGUIForUpload()
    {
        this.Label1.Text = "Uploading document to Scribd";
    }
 
  private void UploadDocumentToScribd(string filename, bool isPrivate)
    {
          PrepareGUIForUpload();
        UploadFileToScribd(filename, GetScribdAccessType(isPrivate));
    }

    private AccessTypes GetScribdAccessType(bool isPrivate)
    {
          return ((isPrivate) ? AccessTypes.Private : AccessTypes.Public);
    }

    private void UploadFileToScribd(string filename, AccessTypes accessType)
    {
        Scribd.Net.Document.Upload(filename, accessType);
    }

    //Called by Scribd API to show progress
    void Document_UploadProgressChanged(object sender, System.Net.UploadProgressChangedEventArgs e)
    {
         this.lblShowPerc.Text = e.ProgressPercentage.ToString();
    }

    void Document_Saved(object sender, DocumentEventArgs e)
    {
        ShowSaveAndUploadFinished();
    }

    private void ShowSaveAndUploadFinished()
    {
        this.Label1.Text = "Document uploaded to Scribd";
    }

    void Document_Uploaded(object sender, DocumentEventArgs e)
    {
        if (e.Document != null)
        {
            e.Document.Title = _title;
            e.Document.Save();
        }
    }
}
Coordinator
Nov 14, 2008 at 12:19 PM
Pinakee,

Try putting a Try...Catch around:

    private void UploadFileToScribd(string filename, AccessTypes accessType)
    {
try{
        Scribd.Net.Document.Upload(filename, accessType);
}
catch(Exception _ex)
{
Response.Write(_ex.ToString());
}
    }

And sending me the content of the exception.  That will help me determine what's going on. 

- Paul