Topbar
Topbar
Sign in
|
Join
|
Client Center
Home
Products
Client Center
Contact
Purchase
Support forums
»
Products
»
Ajax Uploader
»
Re: Can't find items in Attachments
Can't find items in Attachments
Last post 08-17-2009, 4:52 AM by
Raime910
. 3 replies.
Sort Posts:
Oldest to newest
Newest to oldest
Previous
Next
08-16-2009, 5:34 AM
54718
Raime910
Joined on 08-14-2009
Posts 9
Can't find items in Attachments
Reply
Quote
I'm using a persisted upload control to upload multiple files on my web server. But whenever I submit my changes I get an object reference error. I don't know what I'm missing. Here are my codes
using
System;
using
System.IO;
using
CuteWebUI;
using
System.Data;
using
System.Web.UI.WebControls;
public
partial
class
Controls_Uploader_FileUploader : System.Web.UI.UserControl
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
ItemPictureAttachments.ShowCheckBoxes =
false
;
ItemPictureAttachments.TempDirectory =
"~/shop/uploads/temp/"
;
}
protected
override
void
OnInit(EventArgs e)
{
SampleUtil.SetPageCache();
base
.OnInit(e);
if
(!Page.IsPostBack)
{
if
(Request.QueryString[
"si"
] !=
""
&& Request.QueryString[
"si"
] !=
null
)
{
PopulateUploader(
int
.Parse(Request.QueryString[
"si"
].ToString()));
}
}
ItemPictureAttachments.AttachmentRemoveClicked +=
new
AttachmentItemEventHandler(ItemPictureAttachments_DeleteAttachment);
ItemPictureUploader.FileUploaded +=
new
UploaderEventHandler(ItemPicture_FileUploaded);
ItemPictureAttachments.InsertButton.Style[
"display"
] =
"none"
;
}
private
void
PopulateUploader(
int
id)
{
//GET PICTURE DATATABLE FROM DATABASE
ShopItemDL sidl =
new
ShopItemDL();
DataSet ds =
new
DataSet();
ShopItem si =
new
ShopItem();
DataTable dtImageFilename =
new
DataTable();
DataTable dtImageFolder =
new
DataTable();
string
sImageFolder;
si.ShopItemId = id;
ds = sidl.SelectShopItemPictures(si);
dtImageFilename = ds.Tables[0];
dtImageFolder = ds.Tables[1];
sImageFolder = dtImageFolder.Rows[0][
"ShopItemPicFolder"
].ToString();
int
i = 0;
foreach
(DataRow dr
in
dtImageFilename.Rows)
{
// ADD THE IMAGES THAT WERE UPLOADED BEFORE FOR ITEM EDITING
string
str = Server.MapPath(sImageFolder + dr[
"ShopItemPicFilename"
].ToString());
using
(System.IO.FileStream streamImg =
new
System.IO.FileStream(str, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read))
{
ItemPictureAttachments.Items.Add(
int
.Parse(dr[
"ShopItemPicSize"
].ToString()), dr[
"ShopItemPicFilename"
].ToString(), streamImg);
Image imgPicture = ItemPictureAttachments.Items[i].FindControl(
"imgPicture"
)
as
Image;
imgPicture.ImageUrl = Page.ResolveUrl(sImageFolder + dr[
"ShopItemPicFilename"
].ToString());
i++;
}
}
}
private
void
ItemPictureAttachments_DeleteAttachment(
object
sender, AttachmentItemEventArgs args)
{
if
(Request.QueryString[
"si"
] !=
""
&& Request.QueryString[
"si"
] !=
null
)
{
// GET PICTURE DATA
ShopItemDL sidl =
new
ShopItemDL();
int
itemId =
int
.Parse(Request.QueryString[
"si"
]);
string
filename = args.Item.FileName;
string
picfolder = sidl.SelectShopItemPicFolder(itemId);
// DELETE FILE FROM THE SERVER AND DELETE ROW FROM THE DATABASE IF IT EXISTS
if
(File.Exists(Server.MapPath(picfolder + filename)))
{
ShopItemPic sip =
new
ShopItemPic();
sip.ShopItemPicFilename = filename;
sip.ShopItemId = itemId;
sidl.DeleteShopItemPicture(sip);
File.Delete(Server.MapPath(picfolder + filename));
}
}
}
private
void
ItemPicture_FileUploaded(
object
sender, UploaderEventArgs args)
{
if
(GetVisibleItemCount() >= 5)
return
;
// CHECK AND SEE IF THE IMAGE HAS ALREADY BEEN UPLOADED BEFORE
if
(Request.QueryString[
"si"
] !=
""
&& Request.QueryString[
"si"
] !=
null
)
{
foreach
(AttachmentItem item
in
ItemPictureAttachments.Items)
{
if
(item.FileName ==
"x_"
+ Request.QueryString[
"si"
] +
"_"
+ args.FileName || item.FileName == args.FileName)
{
lblConfirm.Visible =
true
;
lblConfirm.Text +=
"<br />"
+ args.FileName +
" already exists."
;
}
}
}
using
(System.IO.Stream stream = args.OpenStream())
{
ItemPictureAttachments.Upload(args.FileSize, args.FileName, stream);
}
}
protected
override
void
OnPreRender(EventArgs e)
{
base
.OnPreRender(e);
if
(GetVisibleItemCount() >= 5)
{
ItemPictureUploader.InsertButton.Enabled =
false
;
}
else
{
ItemPictureUploader.InsertButton.Enabled =
true
;
}
}
int
GetVisibleItemCount()
{
int
count = 0;
foreach
(AttachmentItem item
in
ItemPictureAttachments.Items)
{
if
(item.Visible)
count++;
}
return
count;
}
}
And this is where I get Object reference not set to and instance error..
UploadAttachments ItemPictureAttachments = (UploadAttachments)arcFileUploader.FindControl(
"ItemPictureAttachments"
);
// ITEM ID OF THE PRODUCT BEING EDITED OR HAS BEEN ADDED TO THE DATABASE
// GETTING READY TO UPLOAD THE FILES TO THE SERVER
if
(itemId > 0)
{
if
(ItemPictureAttachments.Items.Count > 0)
{
if
(!Directory.Exists(HttpContext.Current.Server.MapPath(si.ShopItemPicFolder.ToString())))
Directory.CreateDirectory(Server.MapPath(si.ShopItemPicFolder.ToString()));
foreach
(AttachmentItem item
in
ItemPictureAttachments.Items)
{
if
(!File.Exists(Server.MapPath(ViewState[
"picfolder"
] + item.FileName)))
{
ShopItemPic sipic =
new
ShopItemPic();
sipic.ShopItemId = itemId;
//I GET AN ERROR IN THIS LINE
sipic.ShopItemPicFormat = item.FileName.Substring(item.FileName.LastIndexOf(
"."
) + 1, 3);
sipic.ShopItemPicFilename =
"x_"
+ itemId +
"_"
+ item.FileName;
sipic.ShopItemPicSize = item.FileSize;
int
picresult = sidl.InsertShopItemPicture(sipic);
if
(picresult != -1)
{
item.CopyTo(Server.MapPath(si.ShopItemPicFolder + sipic.ShopItemPicFilename));
}
//You can also open the uploaded file's data stream.
//System.IO.Stream data = item.OpenStream();
}
}
}
}
Any ideas?
Thanks,
- Ryan
08-16-2009, 3:27 PM
54721
in reply to
54718
Raime910
Joined on 08-14-2009
Posts 9
Re: Can't find items in Attachments
Reply
Quote
Okay the problem was this;
ItemPictureAttachments.TempDirectory =
"~/shop/uploads/temp/";
I removed it and it worked.
Thanks,
-Ryan
08-16-2009, 9:27 PM
54724
in reply to
54721
cutechat
Joined on 07-22-2004
Posts 2,332
Re: Can't find items in Attachments
Reply
Quote
Ryan,
You should set that property in the Init event, and add the item in the Load event
Regards,
Terry
08-17-2009, 4:52 AM
54733
in reply to
54724
Raime910
Joined on 08-14-2009
Posts 9
Re: Can't find items in Attachments
Reply
Quote
Thanks Terry, another thing.. I have this function that resize images before it copies the image to my server;
private
void
ItemPicture_FileUploaded(
object
sender, UploaderEventArgs args)
{
if
(GetVisibleItemCount() >= 5)
return
;
using
(System.IO.Stream stream = args.OpenStream())
{
ItemPictureAttachments.Upload(args.FileSize, args.FileName, ResizeFromStream(640, stream));
// ItemPictureAttachments.Items.Add(args.FileSize, args.FileName, stream);
}
}
public
Stream ResizeFromStream(
int
MaxSideSize, Stream Buffer)
{
int
intNewWidth;
int
intNewHeight;
System.Drawing.Image imgInput = System.Drawing.Image.FromStream(Buffer);
// GET IMAGE FORMAT
ImageFormat fmtImageFormat = imgInput.RawFormat;
// GET ORIGINAL WIDTH AND HEIGHT
int
intOldWidth = imgInput.Width;
int
intOldHeight = imgInput.Height;
// IS LANDSCAPE OR PORTRAIT ??
int
intMaxSide;
if
(intOldWidth >= intOldHeight)
{
intMaxSide = intOldWidth;
}
else
{
intMaxSide = intOldHeight;
}
if
(intMaxSide > MaxSideSize)
{
// SET NEW WIDTH AND HEIGHT
double
dblCoef = MaxSideSize / (
double
)intMaxSide;
intNewWidth = Convert.ToInt32(dblCoef * intOldWidth);
intNewHeight = Convert.ToInt32(dblCoef * intOldHeight);
}
else
{
intNewWidth = intOldWidth;
intNewHeight = intOldHeight;
}
// CREATE NEW BITMAP
Bitmap bmpResized =
new
Bitmap(imgInput, intNewWidth, intNewHeight);
// SAVE BITMAP TO STREAM
MemoryStream imgStream =
new
MemoryStream();
bmpResized.Save(imgStream, imgInput.RawFormat);
// RELEASE RESOURCES
imgInput.Dispose();
bmpResized.Dispose();
Buffer.Close();
return
imgStream;
}
But for some reason it return a file with 0 bytes and copies this empty file into my server. What am I missing?
Thanks,
- Ryan