SlickUpload
Welcome Guest Search | Active Topics | Log In | Register

UploadHttpRequest Options
mholmen
#1 Posted : Wednesday, September 21, 2011 4:25:50 PM
Rank: Newbie

Groups: Registered

Joined: 9/21/2011
Posts: 3

How do I go about passing either a querystring or variables into my CustomfileUploadStreamProvider?

I see in the docs that UploadHttpRequest as a QueryStrin gattribute, however I only get UploadProvider Information not my querystring

QueryString NameValueCollection Gets the collection of HTTP query string variables.

 

Web.Config:

    <slickUpload>
        <uploadProfiles>
            <add name="noOverwrite">
                <uploadStreamProvider type="MyCustomFileUploadStreamProvider, App_Code" location="~/Uploaded" QU="" />
            </add>
            <add name="Overwrite">
                <uploadStreamProvider type="MyCustomFileUploadStreamProvider, App_Code" location="~/Uploaded" existingAction="Overwrite"/>
            </add>
        </uploadProfiles>
    </slickUpload>

Here is my Custom Stream:

Imports System
Imports System.IO
Imports System.Web.Configuration
Imports Krystalware.SlickUpload
Imports Krystalware.SlickUpload.Web
Imports Krystalware.SlickUpload.Configuration
Imports Krystalware.SlickUpload.Storage


Public Class MyCustomFileUploadStreamProvider
    Inherits Krystalware.SlickUpload.Storage.FileUploadStreamProvider

    Private _location As String

    Public Sub New(ByVal settings As UploadStreamProviderElement)
        MyBase.New(settings)

        _location = settings.Parameters("location")   
    End Sub

    Public Overrides Function GetWriteStream(ByVal file As Krystalware.SlickUpload.UploadedFile) As System.IO.Stream
        'Return MyBase.GetWriteStream(file)

        file.ServerLocation = GetServerFileName(file)

        Dim imp As Impersonate = New Impersonate()

        If imp.StartImpersonation(WebConfigurationManager.AppSettings("USER"), WebConfigurationManager.AppSettings("DOMAIN"), WebConfigurationManager.AppSettings("PASS")) Then
            Dim fileS As FileStream = Nothing

            Try
                fileS = System.IO.File.OpenWrite(file.ServerLocation)

                Return fileS
            Catch ex As Exception
                If fileS IsNot Nothing Then
                    fileS.Dispose()
                End If

                Return Nothing
            End Try
        End If
        Return Nothing
    End Function

    Public Overrides Function GetServerFileName(ByVal file As UploadedFile) As String
        Dim fileName As String = GenerateFileName(file)

        Return Path.Combine(HttpContext.Current.Server.MapPath(_location), fileName)
    End Function

    Public Function GenerateFileName(ByVal file As UploadedFile) As String
        Dim fileName As String = file.ClientName
        Dim InvalidChars As String() = New String() {"$", "&", "+", ",", "/", ";", _
         ":", "=", "?", "@", "<", ">", _
         "{", "}", "|", "\", "^", "~", _
         "[", "]", "`", " "}

        For Each s As String In InvalidChars
            fileName = fileName.Replace(s, "_")
        Next

        fileName = fileName.Replace("..", ".")
        fileName = fileName.Replace("__", "_")

        Return System.DateTime.Now.ToString("yyyyMMdd_hhmmss_") + fileName
    End Function
End Class

 

Axosoft
#2 Posted : Wednesday, September 21, 2011 4:32:35 PM
Rank: Administration


Groups: Administrators

Joined: 7/7/2005
Posts: 1,586
Location: Scottsdale, AZ

A couple questions:

  • What information are you trying to pass in?
  • Where are you trying to pass it from? The page, server side? The client code, based on user input?

mholmen
#3 Posted : Wednesday, September 21, 2011 4:43:14 PM
Rank: Newbie

Groups: Registered

Joined: 9/21/2011
Posts: 3

I need to create the new file name based on a Project Number and Version.

It will be on the server side, but it needs the querystring variables to retrieve. 

Axosoft
#4 Posted : Wednesday, September 21, 2011 4:52:56 PM
Rank: Administration


Groups: Administrators

Joined: 7/7/2005
Posts: 1,586
Location: Scottsdale, AZ

The easiest way to pass data like this is to set the values you need into the Data dictionary of the SlickUpload control on your page. Something like (assuming your SlickUpload control is called slickUpload1):

    slickUpload1.Data["projectNumber"] = "some value";
    slickUpload1.Data["projectVersion"] = "some value";

Then, you can read them out in the upload stream provider GetWriteStream method like this:

    string projectNumber = file.UploadRequest.Data["projectNumber"];
    string projectVersion = file.UploadRequest.Data["projectVersion"];

This feature was just added in v6.0.7 and isn't fully documented yet. If you have any questions or run into any snags, let me know.

mholmen
#5 Posted : Wednesday, September 21, 2011 5:56:34 PM
Rank: Newbie

Groups: Registered

Joined: 9/21/2011
Posts: 3

Thank you Chris.

I just implemented that and it works perfectly.

One more quetion.

In this upload I am using profile "noOverwrite" ( see below ),

1) Even though I have not provided the  < existingAction="Overwrite"/ > attribute, it still overwrites the file.  Is there somewhere else I need to set this so it will NOT overwrite the file.

2) Once I fix the above how do I show the user that the file they are trying to upload exists and it is not overwritten?  Is there a way? 

            <add name="noOverwrite">
                <uploadStreamProvider type="MyCustomFileUploadStreamProvider, App_Code" location="~/Uploaded" />
            </add>

Axosoft
#6 Posted : Thursday, September 22, 2011 8:16:28 AM
Rank: Administration


Groups: Administrators

Joined: 7/7/2005
Posts: 1,586
Location: Scottsdale, AZ

This is because you've overridden GetWriteStream, but you're not calling the base class implementation. The GetWriteStream method in the base class is where the overwrite detection is done. There are a couple options:

 - You could change this line:

    fileS = System.IO.File.OpenWrite(file.ServerLocation);

to this:

    fileS = System.IO.File.OpenWrite(file.ServerLocation, FileMode.CreateNew);

This will cause an exception to be thrown when the file exists. That will cancel the upload, and then you can check for that exception in the session.AllErrors collection. Or you could catch the exception instead, and write a value to file.UploadRequest.Data, and then retrieve that later.

 - You could call the base class method inside the impersonation, like this:

Public Overrides Function GetWriteStream(ByVal file As Krystalware.SlickUpload.UploadedFile) As System.IO.Stream
    Dim imp As Impersonate = New Impersonate()

    If imp.StartImpersonation(WebConfigurationManager.AppSettings("USER"), WebConfigurationManager.AppSettings("DOMAIN"), WebConfigurationManager.AppSettings("PASS")) Then
        Return MyBase.GetWriteStream(file)
    End If

    Return Nothing
End Function

This will allow the overwrite detection in the base class to execute.

Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.